试图遵循Angular Decorator指南(https://docs.angularjs.org/guide/decorators)中的模板,我尝试创建一个指令并对其进行装饰。为什么我装饰的指令不会覆盖AngularJS中的原始方法?
该指令应显示当前日期/时间。我添加了一个(无用的)装饰器来修改link
函数,以便指令显示字符串“today”而不是日期/时间。
出于某种原因,似乎我被覆盖的link
函数没有被调用。原来被调用。为什么是这样?
代码为http://plnkr.co/edit/uvtBiN5vNSjk5I89t99C?p=preview(及以下):
angular.module('app', []);
angular.module('app').directive('myElement', function(){
return {
template: 'Today is {{ date }}',
link: function(scope, element, attrs){
console.log('original link called')
scope.date = new Date();
}
}
})
angular.module('app').decorator('myElementDirective', function($delegate){
$delegate[0].link = function(scope){
console.log('calling delegate link')
scope.date = "today"
}
return $delegate;
})