【问题标题】:Second AngularJS directive not loading from html attribute第二个 AngularJS 指令未从 html 属性加载
【发布时间】:2017-05-05 23:03:18
【问题描述】:

我有这个指令可以避免输入被标记为$pristine:

(function () {
  'use strict';
  angular
      .module('blocks.noDirtyCheck', [])
      .directive('noDirtyCheck', function() {
        // Interacting with input elements having this directive won't cause the
        // form to be marked dirty.
        return {
          restrict: 'A',
          //require: 'ngModel',
          link: function(scope, elm, attrs, ctrl) {
            ctrl.$pristine = false;
          }
        };
      });
})();

还有另一个,它定义了一个自定义小部件,<workflow-input>

(function () {
  'use strict';

  angular
    .module('app.widgets')
    .directive('workflowInput', workflowInput)
    .controller('WorkflowInputController', WorkflowInputController);

  /* @ngInject */
  function workflowInput() {
    return {
      restrict: 'E',
      scope: {
        selectedWorkflow: '=ngModel'
      },
      controller: WorkflowInputController,
      controllerAs: 'vm',
      templateUrl: '/client/app/widgets/workflowInput.html'
    };
  }
...
})();

我想这样使用它:

<workflow-input ng-model="vm.asset.workflows" no-dirty-check></workflow-input>

我知道noDirtyCheck 指令不完整,因为它没有直接应用于实际的&lt;input&gt; 并且需要修复,但这不是当前的问题,问题是noDirtyCheck 指令永远不会被调用。我在workflowInput 控制器上放了一个断点,我可以看到该指令未列在nodeLinkFn 的元素controllerDirectives 中(在角度的代码库中)。那里只有ngModelworkflowInput

有人知道为什么会发生这种情况吗?谢谢

【问题讨论】:

  • 它应该可以工作,所以肯定有其他事情发生。你能提供一个minimal reproducible example 来证明这个问题吗?你确定你已经注入了blocks.noDirtyCheck 模块吗?
  • 你说得对,我忘了注入它。对不起

标签: angularjs angularjs-directive angular-directive


【解决方案1】:

我忘记将模块注入应用程序。谢谢@Lex

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2017-03-25
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多