1

如果链接功能没有被命名,如何一个derocate指令。更具体地说,我想装饰select-ui指令。这是他们的指令是如何定义的:用未命名的链接功能装饰指令

.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? 
       } 
      } 
    } 

}); 

这是我试过了,但它给我的错误Cannot read property 'apply' of undefined,因为链接功能并不在指令命名为:

.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没有被定义,这是主compule函数中的参数。我猜适用不传递参数到功能莫名其妙......

回答

1

做一些这样类似这样的扩展功能编译:

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

我遇到一些问题,请看到更新的问题:) –

+0

哎呀,对不起,当然。检查更新的代码。 – dfsq