9

再利用可能なコントロールを作成するために、AngularJS ディレクティブに OOP 継承を実装しようとしています。継承にはBase2 の Class 定義を使用しています。私が考えていたのは、このようなディレクティブを実装することでした

<control-input type="text" name="vendor_name"></control-input>

次にBaseControl、共通機能のクラスを作成します

angular.module('base',[]).factory('BaseControl', function() {
  return Base.extend({
    'restrict': 'E',
    'require': '^parentForm'
    /* ... */
  };
});

そして、特定のコントロールを作成します

angular.module('controls',['base']).factory('TextControl', function(BaseControl) {
  return BaseControl.extend({
    /* specific functions like templateUrl, compile, link, etc. */
  };
});

問題は、単一のディレクティブを使用して属性でタイプを指定したいということですcontrol-inputが、問題は、ディレクティブを作成するときに、タイプを取得する方法がわからないことです

angular.module('controls',['controls']).directive('control-input', function(TextControl) {
   /* here it should go some code like if (type === 'text') return new TextControl(); */
});

何か案は?

4

1 に答える 1

1

リンク関数のパラメーターを使用してattrs、各ディレクティブのタイプを取得できます。以下のコードを見て、コンソールを確認してください。( http://jsbin.com/oZAHacA/2/ )

<html ng-app="myApp">
  <head>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
   <script>
      var myApp = angular.module('myApp', []);

     myApp.directive('controlInput', [function () {
        return {
          restrict: 'E',
          link: function (scope, iElement, iAttrs) {
              console.log(iAttrs.type);
          }
        };
     }]);

    </script>
  </head>
 <body>

    <control-input type="text" name="vendor_name"></control-input>

 </body>
</html>
于 2013-10-04T01:04:28.590 に答える