简体   繁体   English

AngularJS:数据绑定和点符号

[英]AngularJS: Databinding and dot-notation

I think I just lost some hours to the dot-notation in AngularJS. 我想我只是在AngularJS中花了点时间。

This Plunker demonstrates the problem which still irritates me: 这个Plunker演示了仍然令我烦恼的问题:

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). 我确实知道在使用ng-model时应该使用对象(点符号有助于解决搜索嵌套范围时的参考问题)。 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 我更加惊讶,因为如果我使用数组(并使用push / splice对其进行修改),对对象的引用保持不变: 另一个Plunker

Could someone explain why exactly the databinding does not work for the value anymore when i reassign it? 有人可以解释为什么当我重新分配数据绑定时,数据绑定对于它的value不再起作用了吗? Is there a way to actually bind to the value without passing a wrapper-object from the service? 有没有一种方法可以实际绑定到值,而无需从服务传递包装对象?

This is because whenever you return an object from a function in Javascript, you are actually returning a reference to an object. 这是因为每当您从Java函数中返回对象时,实际上就是在返回对对象的引用。 In your example 在你的例子中

this.getObject = function(){
    return output;
};

the reference to object is returned. 返回对object的引用。 And any changes made to this reference will reflect in the actual object and that is why the data binding works for this. 对此引用所做的任何更改都将反映在实际对象中,这就是为什么数据绑定对此有效的原因。

Whereas, when you return a string from a function, you are returning the value, not the reference to the actual string. 而从函数返回字符串时,则返回的是值,而不是对实际字符串的引用。

this.getVal = function(){
    return output.string;
};

Here you are returning a string when you return output.string which has value "init" . 在这里,当您返回具有值"init" output.string时,您将返回一个字符串。 Any modification done to object.string is not going to change the returned string anymore and that is why it the data binding is failing for value . object.string所做的任何修改都不会再更改返回的字符串,这就是为什么数据绑定的value失败。

See this answer for more information. 有关更多信息,请参见此答案

the function getVal returns the value of output.string AT THAT POINT IN TIME. 函数getVal将在该时间点返回output.string的值。 you then assign this value to your variable $scope.val. 然后将该值分配给变量$ scope.val。 so at that point in time output.string is "Init" hence the value of $scope.val is "Init" This will never change, why would it ? 所以在那个时候output.string是“ Init”,因此$ scope.val的值是“ Init”这将永远不会改变,为什么呢? This has nothing to do with angularjs 2-way data-binding or dot notation. 这与angularjs 2路数据绑定或点表示法无关。 You simply assign the value "Init" to $scope.val and never change it again, this is a pure javascript-thing going on here. 您只需将值“ Init”分配给$ scope.val,再也不要更改它,这是纯JavaScript内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM