Angular UI路由器解析数据未定义

Angular UI-router resolve data gets undefined

本文关键字:数据 未定义 UI 路由器 Angular      更新时间:2023-09-26

我正在遵循ui路由器wiki上与使用解析相关的说明。如果我按照wiki上的说明操作,我会将值共享到控制器中而不会出现问题,如果我通过引用包含控制器,则该值是未定义的。我错过了什么?

.state('intro', {
            url: '/',
            resolve:{
                resA:  function(){
                    return {'value': 'A'};
                }
            },
        views: {
            'content': {
                templateUrl: 'views/home.html',
                controller: function($scope, resA){
                  $scope.resA = resA.value;
                }
            },
            'navigation': {
                templateUrl: 'views/navigation.html'
            }
        } })

    .controller('introController', ['$scope', function($scope,resA) {
    $scope.resA = resA.value; //undefined

这是因为您的数组表示法中没有包含resA,它应该是这样的:

.controller('introController', ['$scope', 'resA', function($scope, resA) {
    $scope.resA = resA.value;
}]);

有一个的工作示例

我们必须声明这个控制器,例如作为状态def:的一部分

'navigation': {
     template: '<div>navi - resA: {{resA}}</div>',
     controller: 'introController',
},

然后我们必须正确地传递参数("resA"也必须传递)

 .controller('introController', ['$scope', 'resA'
 , function($scope,resA) {
    $scope.resA = resA.value; //undefined
 }])
;

工作柱塞