Tag Archives: binding

AngularJS 1.6 Bindings Inconsistent? No, just RTFM.

When upgrading our major project from AngularJS 1.5 to 1.6, I read through the release notes when I was fatigued and the impact of the changes to $compile didn’t sink in.

As changes are made to this large project, code is being adapted towards better Angular (2+) compatibility. I added a new controller in TypeScript and used the controller as approach of binding to the controller object rather than $scope. Without thinking what I was doing, I set a default value for a bound class variable in the class definition:

class MyController {
    public somethingAwesome: string = DefaultStringValue

In the template HTML:

<input ng-model="$ctrl.somethingAwesome">

And “somethingAwesome” became undefined no matter how  much anybody pounded on the keyboard.

The release notes for $compile state, “Due to bcd0d4, pre-assigning bindings on controller instances is disabled by default”, and reminds us, “Initialization logic that relies on bindings being present should be put in the controller’s $onInit<() method, which is guaranteed to always be called after the bindings have been assigned."

Having been chastised by the documentation for my lapse, I sheepishly added an $onInit() method to the class and quietly checked it in.