【问题标题】:How do i access the new $scope after moving with $state.go() in AngularJS UI Router在 AngularJS UI 路由器中使用 $state.go() 移动后如何访问新的 $scope
【发布时间】:2018-03-26 04:01:05
【问题描述】:

大家好,在我使用 $state.go 更改状态后,我正试图达到新的范围。

$state.go("newState").then(function () {
      // doesn't work
     $state.current.$scope.variable = "abc";
     
     // doesn't work as well because it's referencing the old $scope
     $scope.variable = "def";
                })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

【问题讨论】:

    标签: angularjs promise angular-ui-router


    【解决方案1】:

    您可以改为在状态更改中传递变量

    $state.go("newState", {variable: "abc"})
    

    在状态配置中添加参数

    $stateProvider
    .state('newState', {
        url: '/newState',
        params: {
            variable : null
        },
        templateUrl: 'newState.html',
        controller: 'newStateCtrl'
    });
    

    然后在你的 newStateCtrl 中,注入 $stateParams 并获取变量

    $stateParams.variable
    

    【讨论】:

    • 啊,我想我可以避免走这条路。
    猜你喜欢
    • 2017-07-04
    • 1970-01-01
    • 2016-01-13
    • 2013-07-18
    • 2023-03-30
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多