gpt4 book ai didi

javascript - 将另一个(父) Controller 逻辑注入(inject)派生状态(angularjs ui-router)

转载 作者:行者123 更新时间:2023-11-28 05:29:18 25 4
gpt4 key购买 nike

我遇到了一个问题。我有一个模板 URL,但有很多状态变体。我有一个父状态,其他状态是子状态。现在看起来像这样:

 angular
.module("StN")
.config(config);

function config($stateProvider) {
$stateProvider
.state("invoice", {
url: "/invoice",
templateUrl: "/Scripts/js/stn/invoice/invoice.html",
controller: "invoiceController as vm"
})
.state("invoice.custom", {
url: "/castom"
})
.state("invoice.search", {
url: "/search?Status?Invoice?"
});
}

在 Controller 中,我有 switch case 构造,我在其中检查当前状态名称并执行一些差异逻辑

angular
.module("StN")
.controller("invoiceController", invoiceController);

function invoiceController($http, $state) {
switch (state.name) {
case "invoice.search":
//first logic
break;
case "invoice.custom":
//second logic
break;
case "invoice.status":
//third logic
break;
}
}

我需要为每个状态设置不同的 Controller ,但是该 Controller 必须具有父 Controller (invoiceController)的逻辑。我怎样才能做到这一点?

示例

.state("invoice", {
url: "/invoice",
templateUrl: "/Scripts/js/stn/invoice/invoice.html",
controller: "invoiceController as vm"
})
.state("invoice.custom", {
url: "/castom"
controller: "DerivedinvoiceController as vm" //but it must have logic of base controller
})

最佳答案

尽你所能。将此逻辑分离到工厂,并在父 Controller 及其子 Controller 中使用此工厂。

关于javascript - 将另一个(父) Controller 逻辑注入(inject)派生状态(angularjs ui-router),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853526/

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