【发布时间】:2015-09-02 03:59:50
【问题描述】:
我正在尝试将角度指令与原型或对象初始化程序一起使用。例子:
原型:
'use strict';
var Example = function Example($scope, $elem) {
this.$scope = $scope;
this.$elem = $elem;
this.init();
};
Example.prototype.init = function() {
/// rest
};
var exampleDirective = function() {
return {
restrict: 'M',
link: function($scope, $elem) {
return new Example($scope, $elem);
}
};
};
angular
.module('adminApp')
.directive('exampleDirective', exampleDirective);
对象初始化器:
'use strict';
var Example = {
init: function($scope, $elem) {
this.$scope = $scope;
this.$elem = $elem;
// rest
}
};
var exampleDirective = function() {
return {
restrict: 'M',
link: function($scope, $elem) {
return Example.init($scope, $elem);
}
};
};
angular
.module('adminApp')
.directive('exampleDirective', exampleDirective);
两者都有效,但我想弄清楚我应该如何使用,如果有约定的话。
我已经搜索了很多关于使用这样的角度指令,我找到的最接近的答案是:Let's Make Full-Ass Angular Directives
我正在寻找一种不同的方法。
谢谢。
【问题讨论】:
-
仅供参考 - 打字稿方法类似于 - github.com/tastejs/todomvc/tree/gh-pages/examples/…
标签: javascript angularjs oop