我正在尝试通过使用$provide.decorator来更改特定供应商指令的行为。
基本指令看起来像这样:
angular
.module('vendor.module')
.controller('vendorCtrl', [$scope, function($scope) {
var vm = this;
vm.baseFunction = function() {
//code I want to replace
}
}])
.directive('vendorDir', function() {
return {
restrict: 'E',
controller: 'vendorCtrl as vm',
scope: {
stuff: '='
},
bindToController: true
}
}
// and this is my decorator
angular
.module('vendor.module')
.config(function($provide) {
$provide.decorator('vendorDirDirective', function($delegate) {
var directive;
directive = $delegate[0];
// how to override vm.baseFunction ?
return $delegate;
});
}当other指令启动时,我的代码成功运行,但我不确定如何替换控制器中的函数。尝试$scope.baseFunction = function() {}不起作用。
如何更改此函数的行为?
发布于 2018-04-06 04:06:20
试试这样的东西
var _baseFunction = scope.baseFunction;
function baseFunctionExtended() {
_baseFunction.apply(this, arguments);
console.log("yay, I'm modified one!");
}
scope.baseFunction = baseFunctionExtended;这将允许调用原始函数,如果不需要,则不调用,并添加更多内容。下一个问题是如何实现这一点。我想尝试像上面那样扩展链接功能。
https://stackoverflow.com/questions/43090958
复制相似问题