简体   繁体   English

在Angular 1.x中,是否在组件中包含指令?

[英]In Angular 1.x, including a directive in a component?

I have a directive for forcing uppercase in input fields which I would like to reuse in multiple components, but I have an error when I try to include in my component. 我有一个强制在输入字段中强制使用大写字母的指令,我想在多个组件中重复使用,但是当我尝试将其包含在组件中时出现错误。

https://docs.angularjs.org/error/ng/areq?p0=fn&p1=not%20a%20function,%20got%20string https://docs.angularjs.org/error/ng/areq?p0=fn&p1=not%20a%20function,%20got%20string

my_page.html my_page.html

....

   <script src="components.js"></script>
   <script src="my_compnenet.js"></script>
....

components.js components.js

(function (angular)
{

   'use strict';
   angular.module('components', [])
   .directive('uppercased', function () {
      return {
         require: 'ngModel',
         link: function (scope, element, attrs, modelCtrl) {
            modelCtrl.$parsers.push(function (input) {
               return input ? input.toUpperCase() : "";
            });
            element.css("text-transform", "uppercase");
         }
      };
   });
})(window.angular);

my_componenet.js my_componenet.js

angular.
  module('app').
  component('my_component', {
    templateUrl: 'my_page.html',
    controller: ['$scope', '$timeout', 'components'],
    bindings: {
      name: '='
    }
  });
})();

you need to inject the directive module to the component module 您需要将指令模块注入组件模块

angular.
  module('app',['components']).
  component('my_component', {
    templateUrl: 'my_page.html',
    controller: ['$scope', '$timeout'],
    bindings: {
      name: '='
    }
  });
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM