I have done this before, but this time dont know what went wrong.
A service:
angular.module('module')
.service("BaseService", function() {
var vm = this;
var testValue = {a:1,b:2};
vm.getTestValue = function(){
return testValue;
}
});
A method in a Controller:
vm.getTest = function(){
console.log(BaseService.testValue) // undefined - cool
console.log(BaseService.getTestValue()) //{a: 1, b: 2} - cool
var value = BaseService.getTestValue();
console.log(value) // {a: 1, b: 2} - cool
value.a = 20; // updated local object
console.log(value) // {a: 20, b: 2} -cool
console.log(BaseService.getTestValue())-- {a: 20, b: 2} -- ??? why
}
Hope my question is clear, why a local variable is getting updated within service and if it behaves like that..what’s the proper way of getting setting functions in services/factories….would it be better to prototyping functions rather appending fuctions in vm.
Source: AngularJS
from Angular Questions https://angularquestions.com/2017/10/04/angular-1-service-variable-getting-updated-via-controller/
via @lzomedia #developer #freelance #web #lzomedia.com
No comments:
Post a Comment