小编tec*_*her的帖子

Angular - 需要 ngModel 并在自定义指令的控制器中使用它,而不是链接函数

谁能告诉我是否可以在自定义 Angular 指令的控制器中要求和使用 ngModel 。我试图远离链接功能。我看到大多数示例都使用链接函数,但我认为必须有某种方法可以在指令控制器内使用它?或者只能在链接函数中访问?我见过的一种方法(如下所示)给了我未定义的信息。不知道还有没有其他方法??我正在尝试验证组件并在错误对象上设置无效的类。

//directive
angular.module('myApp', [])
  .directive('validator', function (){
    return {
      restrict: 'E',
      require: {
           ngModelCtrl: 'ngModel',
           formCtrl: '?^form'
      },
      replace: true,
      templateUrl: 'view.html',
      scope: {},
      controllerAs: 'ctrl',
      bindToController: {
         rows: '=',
         onSelected: '&?' //passsed selected row outside component
         typedText: '&?' //text typed into input passed outside so developer can create a custom filter, overriding the auto
         textFiltered: '@?' //text return from the custom filter
         ngRequired: "=?" //default false, when set to true the component needs to validate …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-directive angular-ngmodel angularjs-validation

5
推荐指数
1
解决办法
2169
查看次数