gpt4 book ai didi

javascript - angularjs中如何实现基类和派生类的概念?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:47:48 26 4
gpt4 key购买 nike

在angular js中如何实现基类和派生类的概念?。我的要求在基本 Controller 中我需要创建一些方法和派生 Controller 我需要覆盖该方法..任何人都可以帮助我吗..谢谢..

最佳答案

1 非继承方式:

I think you can achieve this somehow like this:

function ChildController($injector, $scope) {
$injector.invoke(ParentController, this, {
$scope: $scope
});
}

2 原型(prototype)继承

(需要 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 ,它会为您完成。

关于javascript - angularjs中如何实现基类和派生类的概念?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34716497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com