How to access ngModelController in Angular1.5 components?

When I use angular.component () to create a completely new component that angular 1.5 provides, there is no link function, so the old way to inject ngModelController or any other controllers does not work.

require: 'ngModel', link: function(scope, element, attrs, ctrls)

The above code is for specifying access to ngModelController. How to access it in the component now?

+5
source share
1 answer

Instead of getting an array of ctrls you now get them by name, just like bindings use for:

 class MyComponentController implements ng.IComponentController { public modelCtrl: ng.INgModelController; ... ... // use modelCtrl here // instead of ctrls[0] ... ... } const MyComponent: ng.IComponentOptions = { template: '...', bindings: {...}, require: {modelCtrl: 'ngModel'}, controller: MyComponentController } angular.module('myModule').component('MyComponent', MyComponent); 

Or if you prefer plain JS:

 function MyComponentController() { ... ... // use this.modelCtrl here ... ... } var MyComponent = { template: '...', bindings: {...}, require: { modelCtrl: 'ngModel' }, controller: MyComponentController }; angular.module('myModule').component('MyComponent', MyComponent); 
+3
source

All Articles