小编And*_*ira的帖子

在AngularJS指令中实现继承

我正在尝试在AngularJS指令中实现OOP继承以生成可重用的控件.我正在使用Base2的Class定义进行继承.我在想的是实现这样的指令

<control-input type="text" name="vendor_name"></control-input>
Run Code Online (Sandbox Code Playgroud)

然后我会创建一个BaseControl通用功能的类

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

然后我会创建特定的控件

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

问题是我想使用单个指令control-input并在属性中指定类型,但问题是当我创建指令时,我不知道如何检索类型

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

有任何想法吗?

javascript oop inheritance angularjs angularjs-directive

9
推荐指数
1
解决办法
4467
查看次数