Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

angularjs - bindToController: Object in directives

Directive bindToController can be either boolean or object, the latter is shown here:

myMod.directive('myDirective', {
  controller: 'MyDirectiveController',
  bindToController: {
    name: '@'
  }
});

But the fact that it was not documented raises questions. Why bindToController: { ... } feature was made in the first place? Are there useful scenarios for it?


Despite bindToController wasn't primarily intended for that, it is interesting to see how it is utilized now in angular.component as bindings property to fill the gap between 1.5 and 2.0, while scope bindings remain unused.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

bindToController was originally just a boolean at inception, but was migrated to allow it to be an object to be more explicit about what items/values you are binding to the controller. With it being a boolean it caused some confusion where this syntax removes that confusion about what you are adding to your controller.

The idea with why this was added was to propagate the usage of the controllerAs syntax to move away from $scope especially with the move towards angular2.

The basis for why this was added was to allow the directive injections/property bindings would now be based upon the controller instance instead of of the scope parameter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...