javascript - Scope object doesn't change without $scope in Angularjs -
i've set small fiddle here http://jsfiddle.net/spmft/137/ , wondering if explain me why changing object doesn't work, while changing properties of object, or changing object "$scope" in front works.
the reason try avoid using scope in controller functions easier test.
my real task ng-click="reset(current, master)"
$scope.reset = function (current, master) { angular.copy(current, master); }
this doesn't work, whereas works:
$scope.reset = function (current, master) { angular.copy($scope.current, master); }
both $scope.current , $scope.master exist
cheers
update:
my problem wasn't updating object itself. solve problem use e.g.
angular.extend(data, { name: 'change', game:'change' });
or
angular.copy({ name: 'change', game:'change' }, data);//pay attention order
the reason creating new object (and therefore different reference) , assigning local variable previously pointing same object.
$scope.change = function (data) { data = { name: 'change', game:'change' }; }
the data
variable passed holds reference same object of $scope.data
assigning local variable pointing reference of object a new object b, locally. @ point, $scope.data
still holds reference same object has before. doing changing reference local variable , discarding @ end of function.
in specific case, should work (using current
or $scope.current
). believe inverting parameter in angular.copy
should (source, destination)
. see updated fiddle simple demo.
Comments
Post a Comment