如何在Angular1.5组件中访问ngModelController?

时间:2016-08-10 01:45:05

标签: angularjs-components

当我使用angular.component()创建angular 1.5提供的全新组件时,没有链接功能,因此注入ngModelController或任何其他控制器的旧方法不起作用。 / p>

var today = DateTime.Now.Date; var blogs1 = from c in context.Meal where c.PersonID.Equals(id) where DbFunctions.DiffDays(today, c.Datetime) <= 7 select c; List<Meal> blogs = blogs1.ToList();

以上代码用于访问ngModelController的指令。 我们现在如何在组件中访问它?

1 个答案:

答案 0 :(得分:5)

而不是获取ctrls数组,您现在可以通过名称获取它们,就像bindings用于:

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);

或者,如果您更喜欢简单的JS:

function MyComponentController() {
    ...
    ...
    // use this.modelCtrl here
    ...
    ...
}

var MyComponent = {
    template: '...',
    bindings: {...},
    require: { modelCtrl: 'ngModel' },
    controller: MyComponentController
};

angular.module('myModule').component('MyComponent', MyComponent);
相关问题