Angular BindOnce, what can prevent it to work?

233 views Asked by At

We're currently migrating to Angular 1.3 while testing the bindOnce functionality I encountered this problem: the exact piece of code...

Controller Code:

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

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? 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

2

There are 2 answers

1
Imran Momin On BEST ANSWER

I think the value not changing after the click, bcoz of the context of this is changed for the clickTest function

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.

0
daviddv On

AngularJS 1.3's bind-once does not work when Batarang extension is enabled