angularjs - 1 service - 2 ctrl - how to bind value of the service -
i'm new angularjs. try make little explorer game angularjs
here problem:
when click on button, inventory should showed. when console.log function, can see change of value of showedinventory inventoriesctrl don't.
service
angular.module('inventoryservice', []) .factory('inventoryservice', function(){ var showedinventory = false; return{ getshowinventory: function(){ console.log('getshowinventory'); return showedinventory; }, showinventory: function(){ console.log('showinventory'); showedinventory = true; return showedinventory; } } })
ctrl 1 & 2
app.controller('navigationsctrl',['$scope', 'inventoryservice', function($scope, inventoryservice){ $scope.showinventory = function(){ inventoryservice.showinventory(); }}]); app.controller('inventoriesctrl',['$scope', 'inventoryservice', function($scope, inventoryservice){ $scope.showedinventory = inventoryservice.getshowinventory();}]);
someone can me ? https://jsfiddle.net/s8spyw21/3/
(note
"inventory !"
shoudn't showed)thank !
i forked fiddle , think looking https://jsfiddle.net/dougefresh/ydl81p75/7/
other not having fiddle setup correctly ( needed modify html body tag settings contain <body ng-app="myapp">
, along setting controllers little nicer) comes down understanding how data binding works factories. cannot bind variable, can bind accessor, think trying do.
in controller had following ran function bound scope object, never updated
$scope.showedinventory = inventoryservice.getshowinventory(); // in controller
what needed this:
$scope.showedinventory = inventoryservice.getshowinventory;
that binds method, not result. in view instead of this:
<div ng-show="showedinventory==true">
you should have following, calls getter method every digest cycle
<div ng-show="showedinventory()==true">
and can shortened this
<div ng-show="showedinventory()">
wiki
Comments
Post a Comment