在angular js中如何实现基类和派生类的概念?。我的要求在基本控制器中,我需要创建一些方法,而派生控制器我需要覆盖该方法。。有人能帮我吗。。谢谢
1非继承方式:
I think you can achieve this somehow like this:
function ChildController($injector, $scope) {
$injector.invoke(ParentController, this, {
$scope: $scope
});
}
2原型继承
(需要ES5浏览器支持)
angular.inherits = function (ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false
}
});
};
用法:
function ChildController($scope) {
// Invoke the parent constructor.
ChildController.super_.apply(this, arguments);
}
angular.inherits(ChildController, ParentController);
此处的代码示例:https://github.com/exratione/angularjs-controller-inheritance
3获取Typescript
因此,在这里,您可以更改任何想要附加到$scope的方法。这不是真正的遗产,但总比什么都没有好。
对于继承支持,您可以使用typescript,它会为您完成这项工作。