2013-10-04 13 views
9

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

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

共通の機能についてBaseControlクラスを作成します。そして、私は特定のコントロール

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

作成することになり、この

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

のようなディレクティブを実装することでした問題は、単一の指令control-inputを使用して属性の型を指定することですが、問題はディレクティブを作成するときに、私はkをしません今どのようにタイプを取得する

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

アイデア?

答えて

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> 
+0

はい、問題は、 '{...}' ''返す新しいTextControl(...)による返品など何かを置き換える、私は相続としてDDO自体を返すようにしたいということです当時、私は属性や範囲についての情報がありませんでした。 –

関連する問題