【问题标题】:with Angular UI Router, is it possible for the child state to call a function in the parent state's controller?使用 Angular UI Router,子状态是否可以调用父状态控制器中的函数?
【发布时间】:2015-09-15 21:22:05
【问题描述】:

我可以使用 stateParams 在父控制器中设置值 - 但想在该控制器中调用函数。我是否需要在孩子的“resolve:”或“onEnter:”属性中添加一些东西来调用父母控制器中的函数。注意:子状态没有任何模板,因为它们将使用父模板。

myApp.config(function($stateProvider) {
$stateProvider
    .state('reports', {
        url: "/reports",
        templateUrl: "reports.parent.template.html",
        controller: "ReportsController",
        controllerAs: "reports"
    })
    .state('reports.users', {
        url: "/users",
        reportSettings: {
            title: "Active Users Report",
            reportName: "UserList.trdx"
        }
    })
    .state('reports.departments', {
        url: "/departments",
        reportSettings: {
            title: "Active Departments Report",
            reportName: "DepartmentList.trdx"
        }
    })
});

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    更新:请参阅此 plunkr 以获得解决方案:http://plnkr.co/edit/xnMDiv

    由于您将 ReportsController 注入为“reports”,因此您只需将该控制器中的函数定义为“this.function”,然后在子视图中使用“reports.function”即可访问它。

    如果您想在子控制器(而不是视图 HTML)中调用该函数,则需要在父控制器中创建该函数作为解析并将其注入子控制器。

    所以:

    resolve: { myfunction... }
    

    在你的子控制器中

    ('myfunction')
    

    希望这是有道理的。我是移动的,写作很棘手。

    【讨论】:

    • 感谢您的回复,上面的代码会是什么样子?
    • 我创建了一个 plunkr 文件来帮助解释我正在尝试做什么...plnkr.co/edit/2qOLpbQrvLRQSB4BnjlU?p=info - 这可能是我做错了。
    • (我只是对它进行了一些更改(基本上是想让它在 plunkr 中工作)
    • 请试试这个链接 plnkr.co/edit/tBqm0K?p=preview (之前没有真正使用过 plunkr - 非常酷)
    • 哇太棒了-谢谢! (它似乎忽略了第一次点击——你知道它是如何拾取它的吗?)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    相关资源
    最近更新 更多