admin管理员组文章数量:1429488
SOLUTION: It seemed that my .get() function invoked before I could update the variable 'something' and therefore it didn't show the updated variable.
When I tested it on the real page it worked like a charm :)
final FIDDLE
note: I did add a return to the set function to immediately update the view.
UPDATE: got my factory working alright but can't get the value bound to the factory .set() function. FIDDLE
I’m building a manual to install a USB-network connector and need to store some variables to show the right content across multiple pages (within the same controller tough).
After doing some research it seems 'services' are the right way to do this however I can't get them to work.
It does work when I put the function inside the controller itself but it get's reset to '[]' again when I change page.
function inside controller as I have it right now is:
$scope.isActiveSystem = [];
$scope.activeSystem = function(name) {
$scope.isActiveSystem = name.value;
}
Can someone point me in the right direction how to put this inside a service or give me an example to study so I can understand better what's going on?
Also, should I store my data in a factory or some service?
My attempt to make a service: FIDDLE
SOLUTION: It seemed that my .get() function invoked before I could update the variable 'something' and therefore it didn't show the updated variable.
When I tested it on the real page it worked like a charm :)
final FIDDLE
note: I did add a return to the set function to immediately update the view.
UPDATE: got my factory working alright but can't get the value bound to the factory .set() function. FIDDLE
I’m building a manual to install a USB-network connector and need to store some variables to show the right content across multiple pages (within the same controller tough).
After doing some research it seems 'services' are the right way to do this however I can't get them to work.
It does work when I put the function inside the controller itself but it get's reset to '[]' again when I change page.
function inside controller as I have it right now is:
$scope.isActiveSystem = [];
$scope.activeSystem = function(name) {
$scope.isActiveSystem = name.value;
}
Can someone point me in the right direction how to put this inside a service or give me an example to study so I can understand better what's going on?
Also, should I store my data in a factory or some service?
My attempt to make a service: FIDDLE
Share Improve this question edited Apr 27, 2016 at 8:21 Deblaton Jean-Philippe 11.4k4 gold badges53 silver badges68 bronze badges asked Apr 17, 2015 at 13:35 Noel HeesenNoel Heesen 2431 gold badge4 silver badges14 bronze badges2 Answers
Reset to default 3You're not far from the solution.
Seriously, I used to try to remember the difference between service and factory. In the end, both can be used exactly in the same way, to fulfill the same requirement.
app.factory('dmFactory', function() {
//name.value should equal to system.value from the controller
var somethingIWantToRemember = {};
return {
set: set,
get: get
}
function get(key){ //You can forget about the key if you want
return somethingIWantToRemember[key];
}
function set(key, value){
somethingIWantToRemember[key] = value;
}
});
and in your controller, you just have to call both functions that way :
$scope.something = dmFactory.get(key);
or
dmFactory.set(key, $scope.something);
From your controller, if your reset it when page load, it is normal that your data gets reseted.
You need to : - inject your service into your controller
app.controller('myCtrl',['myService',function(myService){
}]);
- create a get function from your service to retreive the value of the variable
factory.getVal = function(){
return val;
};
- call the get function at start of your controller as
$scope.isActiveSystem = myService.getVal();
本文标签: javascriptAngular store value to service to acces on multiple pagesStack Overflow
版权声明:本文标题:javascript - Angular store value to service to acces on multiple pages - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745482487a2660237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论