当我以角度扩展控制器时,是否有任何方法可以调用"超类"来自"子类的控制器"覆盖它的功能?
为了清楚起见 - 在Java中我会这样做:
class Foo {
void doStuff(){
//do stuff
}
}
class FooBar extends Foo {
void doStuff(){
super.doStuff();
//do more stuff
}
}
我想在角度上做相同的事情
myApp.controller('FooCtrl', function($scope){
$scope.doStuff = function(){
//do stuff
}
}).controller('FooBarCtrl', function($scope){
angular.extend(this, $controller('FooCtrl', {$scope: $scope}));
$scope.doStuff = function(){
// ??? <- INSERT ANSWER HERE
//do more stuff
}
}
答案 0 :(得分:1)
我不推荐这种模式,但作为问题的答案,这是一种方法:
myApp.controller('FooCtrl', function($scope){
$scope.doStuff = function(){
//do stuff
}
}).controller('FooBarCtrl', function($scope){
angular.extend(this, $controller('FooCtrl', {$scope: $scope}));
//extend the scope
var super = angular.extend({}, $scope);
$scope.doStuff = function(){
// ??? <- INSERT ANSWER HERE
//do more stuff
//call the "superclass" methods
if(super.doStuff){
super.doStuff();
}
}
}
Spitballing,我想您可以编写一个帮助程序服务,允许您使用对超类实现的引用来覆盖属性,以使其更清晰。也许是通过压倒这个&#34;。类似的东西:
$scope.doStuff = $override($scope.doStuff, function() {
this(); //calls the original doStuff function
});
.factory('$override', function(){
return function(method, func){
return function(){
return func.apply(method, arguments);
};
};
});
答案 1 :(得分:0)
您可以使用$ parent
所以
$scope.$parent.doStuff()