Please help me understand the behavior of the angular.
There is a service:
myApp.factory('myService', function($http){ var obj = []; function init () { return $http.get('test.php'). then(function(answ) { obj = answ; }); } function getObj () { return obj; } return { init: init, obj1: obj, obj2: getObj } }) And there is a controller that works with this service:
myApp.controller('myController', function myController($scope, myService){ console.log('0.0 ' + myService.obj1); console.log('0.1 ' + myService.obj2()); myService.obj1 = ['test']; console.log('1.0 ' + myService.obj1); console.log('1.1 ' + myService.obj2()); myService.init() .then(function() { console.log('2.0 ' + myService.obj1); console.log('2.1 ' + myService.obj2()); myService.obj1 = ['test1']; console.log('3.0 ' + myService.obj1); console.log('3.1 ' + myService.obj2()); }); As a result, the console will display:
0.0 0.1 1.0 test 1.1 test 2.0 test 2.1 [*данные от сервера*] 3.0 test1 3.1 [*данные от сервера*] I can not understand why this is happening. Obj1 and obj2 come first to refer to the same data, and through obj1 they can be changed and obj2 will notice these changes, but after the data in the service is changed by the service itself, obj1 continues to refer to the old version of the data, and obj2 to the new version , and data changes through obj1 are not visible in obj2 (and vice versa. If you also enter the setter, change data through it, then the changes will be visible in obj2 and not visible in obj1).
Why does obj1 continue to reference the old object?
pushcall for example? - Grundy