我正在开发一个 Angular 应用程序。我需要为所有链接添加特殊行为。在 AngularJS 中只会写一个这样的指令:
angular.module('whatever.module', []).directive('href', function() {
return {
restrict: 'A',
link: function($scope, $element, $attrs) {
// do stuff
}
};
});
在 Angular 中,我可以编写如下指令:
@Directive({
selector: '[href]',
})
export class MyHrefDirective {
constructor() {
// whatever
}
}
但是我怎样才能告诉应用程序在全局范围内使用该指令呢?我有很多关于它们的链接的观点。我是否必须导入它并directives
在每个组件的数组中指定它(这是很多)?
我尝试将它注入到bootstrap
函数中,就像您应该使用服务一样在全局范围内拥有一个实例,但这没有用