Wednesday, December 17, 2014

Angular.js 1.3: bindToController

I keep finding new stuff inside Angular 1.3. One of the things I found is the bindToController attribute. I’m an advocate of using the controllerAs syntax to get rid of the $scope variable. The only problem before is that in a directive you still had to use $scope…until now.

Let’s first have a look at what the AngularJS documentation has to say about the bindToController attribute:

When an isolate scope is used for a component (see above), and controllerAs is used, bindToController: true will allow a component to have its properties bound to the controller, rather than to scope. When the controller is instantiated, the initial values of the isolate scope bindings are already available

Before scope variables were not included in the controller scope when controllerAs was used. This means that the directive's scope variables were not bound to the object created by controllerAs.  The bindToController attribute fixes this.

Enough talk, show me the code:

1 comment:

Raju Kumar said...

This is my first time visit on your site and i have bookmark this for again visit. thanks a lot of for share a appreciable post ..
Angularjs Training In Hyderabad