Javascript 使用未命名链接函数修饰指令
如果链接函数未命名,一个DeLocate指令如何执行。更具体地说,我想修饰selectui指令。他们的指令是这样定义的:Javascript 使用未命名链接函数修饰指令,javascript,angularjs,angularjs-directive,angularjs-select2,Javascript,Angularjs,Angularjs Directive,Angularjs Select2,如果链接函数未命名,一个DeLocate指令如何执行。更具体地说,我想修饰selectui指令。他们的指令是这样定义的: .directive('uiSelect', function() { return { restrict: 'EA', scope: true, compile: function(tElement, tAttrs) { //some code
.directive('uiSelect', function() {
return {
restrict: 'EA',
scope: true,
compile: function(tElement, tAttrs) {
//some code
return function(scope, element, attrs, ctrls){
//some code here
//how do I extend this?
}
}
}
});
我是这样尝试的,但它给了我错误无法读取未定义的属性“apply”,因为链接函数未在指令中命名:
.decorator('uiSelectDirective', function($delegate, Restangular){
var directive;
directive = $delegate[0];
console.log(directive, $delegate);
directive.scope = {
endpoint: '=',
items: '=',
parameters: '='
};
directive.compile = function() {
return function($scope, element, attrs){
directive.link.apply(this, arguments);
// custom code here
}
};
return $delegate;
});
编辑
我尝试了建议的方法,但遇到了问题
.decorator('uiSelectDirective', function($delegate, Restangular){
var directive;
directive = $delegate[0];
directive.scope = {
endpoint: '=',
items: '=',
parameters: '='
};
directive.compile = function(originalCompile) {
var args = arguments;
return function($scope, element, attrs){
console.log($scope); // this here is returning element instead of scope?
return originalCompile.apply(this, args);
}
}(directive.compile);
return $delegate;
});
我得到的不是$scope
而是元素变量([div.ui-select-container.ui-select-bootstrap.dropdown]
)。我还错误地说tAttrs没有定义,这是主计算函数中的参数。我猜apply并没有以某种方式将参数传递给函数…这样做可以扩展编译函数:
directive.compile = function(originalCompile) {
return function() {
var oldLink = originalCompile.apply(this, arguments);
return function($scope, element, attrs) {
// custom code for link here
return oldLink.apply(this, arguments)
}
}
}(directive.compile);
我遇到了一些问题,请查看更新的问题:)