爲什麼globalObj得到修改,爲什麼這個修改對secondo控制器有影響?AngularJS - 如何做一個全局對象,而不是溝通到不同的控制器其修改?
var globalObj = [{..},{..},{..}];
mymod = angular.module("app", []);
//CONTROLLER "first"
mymod.controller("first", function($scope){
$scope.myobj = globalObj;
//do something with $scope.myobj, as add, remove or modify some element
});
//FACTORY
mymod.factory("myfact", function(){
objfact = function(someparam){
this.obj = globalObj;
//do something with this.obj
}
return objfact;
});
//CONTROLLER "second"
mymod.controller("second", function($scope, myfact){
$scope.otherMyObj = globalObj;
myfact.objfact($scope.otherMyObj);
});
您正在分配globalObj的引用。任何人都可以改變它,任何參考的人都會受到影響。 – Chandermani