javascript - Binding to service property only via reference? -
consider service:
app.service('myservice',function(){ this.strprop = ''; this.objprop = { content: '' } })
and controller:
app.controller('myctrl',function($scope,myservice){ $scope.str = myservice.strprop; $scope.obj = myservice.objprop; })
also consider markup binds both scope properties
<input type="text" ng-model="str"> <input type="text" ng-model="obj.content">
when values updated through view via user input, service show changes made object, while string property stays empty.
am right in assuming caused fact object bound via reference , string not?
thats right, in example strprop
bound value.
when passing in primitive type variable string or number, value passed in value. means changes variable while in function separate happens outside function.
passing in object, however, passes in reference. in case, property of object accessible within function.
so, have change code this:
$scope.someprop = myservice;
and pass view:
<input type="text" ng-model="someprop.strprop"> <input type="text" ng-model="someprop.objprop.content">
Comments
Post a Comment