Javascript 使用不同方法的角度指令
我使用的是这样的角度指令:Javascript 使用不同方法的角度指令,javascript,angularjs,angularjs-directive,angular-services,Javascript,Angularjs,Angularjs Directive,Angular Services,我使用的是这样的角度指令: 'use strict'; var eventDirective = { /** * Initialize event directive and return the link * calling all nested methods. * */ init: function($scope, $element) { var that = this; return {
'use strict';
var eventDirective = {
/**
* Initialize event directive and return the link
* calling all nested methods.
*
*/
init: function($scope, $element) {
var that = this;
return {
link: function(scope) {
scope.$watch('events', function() {
if (scope.events === undefined) {
return;
}
/**
* Every time the user access the event page, this methods
* will be called.
*
*/
__TableSorter__.init($element);
});
},
restrict: 'E'
};
},
__TableSorter__: {
init: function(element) {
console.log(element) // PRINTS ELEMENT
}
}
};
angular
.module('adminApp')
.directive('eventDirective', eventDirective.init.bind(eventDirective));
为了举例说明,我创建了这个简单的示例。表排序器将正常运行
问题是当我有几个脚本时,代码太大了。有什么办法解决这个问题吗?也许把脚本放在其他地方作为工厂或服务
我的问题是如何做到这一点。我试图在指令中注入服务,但结果是未定义
谢谢。一个好方法应该是,在定义指令时,可以将
bindToController
设置为true
,并在控制器类中更正逻辑。您可以将您的服务注入该控制器
比如说
myModule.directive('directiveName', function factory(injectables) {
var directiveDefinitionObject = {
template: '<div></div>',
scope: {},
controllerAs: 'yourControllerClass',
bindToController: true
};
return directiveDefinitionObject;
});
myModule.directive('directiveName',函数工厂(可注入){
var directiveDefinitionObject={
模板:“”,
作用域:{},
controllerAs:“你的ControllerClass”,
bindToController:true
};
返回directiveDefinitionObject;
});
yourControllerClass
是这里的角度控制器。hm,我是角度控制器方面的新手。你知道有什么教程可以解释这一点吗?我已经更新了我的答案,下面是文档。在页面上查找bindToController。获取它!让我问你一件事。你觉得这样做怎么样?你怎么看?