简体   繁体   English

UI路由器无法将已解析的对象注入控制器

[英]UI-router can't inject resolved object into controller

I have a list of lists and I'm trying to make a detail (individual) view for each list. 我有一个列表列表,我试图为每个列表制作一个详细的(个人的)视图。 I've successfully changed the URL and retrieved the object from the factory but the object never makes it into the controller/view. 我已经成功更改了URL 从工厂检索了对象,但是该对象从未进入控制器/视图。 When I click on the link for the detail the view changes but $scope.list is undefined. 当我单击详细信息的链接时,视图会更改,但是$ scope.list是未定义的。 What am I doing wrong? 我究竟做错了什么?

Config : 配置

As far as I can tell the error is in the way I am doing the resolve but this is the only thing I tried that didn't throw an error 据我所知,错误是我执行解析的方式,但这是我尝试过的唯一未引发错误的操作

    .config(['$stateProvider', '$urlRouterProvider',
    function($stateProvider, $urlRouterProvider) {

        $stateProvider
        .state('lists', {
            url: '/lists',
            templateUrl: 'views/lists.html',
            controller: 'ListsCtrl',
            resolve: {
                listsPromise: ['lists', function (lists){
                    console.log('LISTS State');
                    return lists.getAll();
                }]
            }
        })
        .state('list', {
            url: '/{id}',
            templateUrl: 'views/list.html',
            controller: 'ListCtrl',
            resolve: {
                list: ['$stateParams', 'lists', function($stateParams, lists){
                    return lists.getList($stateParams.id);
                }]
            },
        });

        $urlRouterProvider.otherwise('/lists');

}]);

Factory : 工厂

.factory('lists', ['$http', 
    function($http){
        var lists = {
            lists: []
        };

        lists.getAll = function(){
            $http.get('/lists').success(function(res){
                angular.copy(res, lists.lists);
            });
        };

        lists.getList = function(id){
            $http.get(id).then(function(res){
                return res.data;
            });
        };

        return lists;
}]);

Controller(s) : 控制器

is it possible to do this with one controller? 一个控制器可以做到这一点吗?

.controller('ListsCtrl', ['$scope', '$http', 'ngDialog', 'lists',
    function($scope, $http, lists) {
    console.log("controller loaded");
    $scope.lists = lists.lists;

}])
.controller('ListCtrl', ['$scope', '$http',
    function($scope, $http, list){
        $scope.list = list;
        console.log(list); //returns undefined
}]);

There are a few problems here that I can see. 我在这里看到一些问题。

  1. You are not returning a promise from either lists.getAll() or lists.getList() . 您不会从lists.getAll()lists.getList()返回承诺。 Without returning a promise, the UI router doesn't know to wait for a promise to be resolved before invoking your controller. 在不返回承诺的情况下,UI路由器不知道在调用控制器之前等待解决承诺。 To fix it, put a return before the $http call in both. 要解决此问题,请在两者中的$http调用之前放置一个return
  2. Invoking the URL id directly in lists.getList() is likely wrong, since it will result in a request to a URL that is just a number, nothing else. 直接在lists.getList()调用URL id可能是错误的,因为这将导致对URL的请求只是一个数字,而没有其他要求。 And it will be a relative URL, too, which you probably don't want. 而且它也是一个相对URL,您可能不希望这样做。
  3. The key in the resolve map/object is actually the name of a local dependency for that controller only, so you should have that as an explicit dependency in your controller. resolve映射/对象中的键实际上仅是该控制器的本地依赖项的名称,因此您应该将其作为控制器中的显式依赖项。 The value of it will be whatever the promise resolves to. 它的价值将是诺言所能解决的。

To fix the last one, change your ListCtrl to as follows: 要修复最后一个,请将ListCtrl更改为以下内容:

 .controller('ListCtrl', ['$scope', '$http', 'list',
     function($scope, $http, list){
         $scope.list = list;
         console.log(list); //returns the correct thing
  }]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM