【问题标题】:How do I reload the child state if there is a change in the parent state controller in AngularJS?如果 AngularJS 中的父状态控制器发生变化,我如何重新加载子状态?
【发布时间】:2016-07-05 20:00:05
【问题描述】:

我正在使用带有 Angular 的 ui-router,我的想法是 UI 布局中的下拉菜单(也是父状态)有一个下拉菜单会更改子状态中的数据。

路由:

.state('App', {
            url : '/',
            controller : 'AppController',
            templateUrl : 'views/layout.html',

        })

.state('App.userProfile', {
            parent : 'App',
            url : 'profile',
            templateUrl : 'views/user-profile.html',
            controller : 'UserProfileController'            
        })

HTML 下拉菜单:

<div class="top-bar">
<div class="top-bar-logo"></div>
<label>Active team</label>
<select class="clean-custom-select margin-bottom-10"
        ng-options="team._id as team.Name for team in userTeams"
        ng-change="changeActiveTeam(ActiveTeamModel)"
        ng-model="ActiveTeamModel">
<option value="">Choose a team</option>


So what I am trying to achieve is when the select dropdown changes the child view will reload with the relevant changes.However, this has to work for all child states.上面的代码只是一个子状态的例子,其实还有很多。

对于更多上下文,下拉列表将包含团队名称。一旦用户选择了一个团队,所有子状态都应该引用该团队。

【问题讨论】:

    标签: javascript angularjs angular-ui-router


    【解决方案1】:

    根据您的要求,您可以使用角度服务 - $emit、$broadcast、$on 在父子层次结构上交换事件和范围更改通知。

    这将帮助您理解这一点 link

    【讨论】:

    • 看来成功了!我使用 $broadcast 服务将布尔值传递给另一个控制器。谢谢你:)
    猜你喜欢
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多