I think I just lost some hours to the dot-notation in AngularJS.
This Plunker demonstrates the problem which still irritates me:
app.controller('MainCtrl', function($scope, ValueService) {
$scope.obj= ValueService.getObject(); // Output: {"string": "New!!!"}
$scope.val = ValueService.getVal(); // Output: "init"
ValueService.setVal();
})
.service('ValueService', function(){
var output= {string: 'init'};
this.setVal = function(val){
output.string = 'New!!!';
};
this.getObject = function(){
return output;
};
this.getVal = function(){
return output.string;
};
return this;
});
I did know that i should use objects when using ng-model
(the dot-notation helps resolving reference issues when searching nested scopes). However I was not aware that this applies to a simple case like this as well. I was even more surprised because the reference to my object stays in tact if i use arrays (and modify it with push/splice): another Plunker
Could someone explain why exactly the databinding does not work for the value
anymore when i reassign it? Is there a way to actually bind to the value without passing a wrapper-object from the service?