简体   繁体   English

Angular BindOnce,什么可以阻止它工作?

[英]Angular BindOnce, what can prevent it to work?

We're currently migrating to Angular 1.3 while testing the bindOnce functionality I encountered this problem: the exact piece of code... 我们目前在测试bindOnce功能时正在迁移到Angular 1.3,我遇到了这个问题:确切的代码...

Controller Code: 控制器代码:

this.value = 'value';
this.clickTest = function () {
    this.value = 'changed';
};

HTML: HTML:

//cm is the ControllerAs variable defined in the route
 <p> {{ ::cm.value}} </p>
 <button ng-click="cm.clickTest()"> CLICK </button>

...works fine in a controller but does not in another one - in the working controller the click event will not change the value, while in the non-working one it will change the value - (the code is exactly the same in both controllers, routing changes and apparently something else changes too). ...在一个控制器中工作正常,但在另一个控制器中工作不正常-在工作控制器中,单击事件不会更改该值,而在非工作控制器中,单击事件将更改该值-(两者中的代码完全相同控制器,路由更改,显然还有其他更改)。 Both controllers were emptied in order to have a better testing environment. 清空两个控制器是为了拥有更好的测试环境。

Now what I'd like to know is: is there something that can prevent the bindOnce functionality to work as expected? 现在我想知道的是:是否有某些东西可以阻止bindOnce功能按预期工作? Is there something that force angular code to not delete the watcher or to enable it again despite the correct syntax (::) has been used in the view? 尽管已在视图中使用了正确的语法(::),是否存在迫使角度代码不删除观察者或再次启用它的问题?

I am working with Angular 1.3.2 我正在使用Angular 1.3.2

I think the value not changing after the click, bcoz of the context of this is changed for the clickTest function 我觉得值bcoz背景下的点击后不改变, this改变对于clickTest功能

Try the code below, hope it works 试试下面的代码,希望它能工作

var vm = this;
vm.value = 'value';
vm.clickTest = function () {
    vm.value = 'changed';
};
return vm;

And for the bindOnce, I don't think its possible to bind again after the value is changed in the controller, and for testing the code, we should not worry about the view. 对于bindOnce,我认为在控制器中更改值后不可能再次绑定,对于测试代码,我们不必担心视图。

启用Batarang扩展后,AngularJS 1.3的bind-once不起作用

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

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