I want to be able to determine which directive is displayed based on a variable from a shared service. This is what I have so far.
main.html
<character-select ng-if="stateChangeService.playerState === 'characterSelect'"></character-select>
<fight-display ng-if="stateChangeService.playerState === 'fight'"></fight-display>
service
angular
.module('outerZone')
.service('stateChangeService', stateChangeService);
function stateChangeService() {
var vm = this;
vm.playerState = 'characterSelect';
}
Currently when I load the page, nothing displays. The service is injected into both the character-select directive and the fight-display directive as well as the MainController. Any ideas?
View in Angular can access variables in current $scope or parent scopes. so you have to assign service to current $scope or $rootScope.
There is no way your view knows anything about your service. You should assign your service to context.
You can only use those variable on view/HTML which are bounded to
$scope
orthis
(while using controllerAs pattern). You should expose yourservice/factory/constant
on view, OR rather better way would write a getter for accessing particular variable from service. Its best practice to expose relevant matter from service. There after you could call that getter from view likegetPlayerState()
HTML
Code