Param默认值来自resolve

时间:2017-09-27 10:15:38

标签: angular-ui-router

如何在resolve默认值中使用param,例如

  params: {
            id: {
                type: 'int',
                value: ['lists', function(lists) {
                    return lists[0].id;  <====
                }],
                squash: false,
                dynamic: false
            }
        },


        resolve: {
            lists: ['MailingListService', function (MailingListService) {
                //load the tree data
                return MailingListService.getMailingLists().then(function (lists) {
                    return lists;
                });
            }],
        },

1 个答案:

答案 0 :(得分:0)

由于参数必须同步处理,因此您无法在默认参数值中使用解析。

但是,您可以使用占位符值(例如null)并稍后将其映射到解析值。

params: {
        id: {
            type: 'int',
            value: null,
            squash: false,
            dynamic: false
        }
    },


    resolve: {
        lists: ['MailingListService', function (MailingListService) {
            //load the tree data
            return MailingListService.getMailingLists().then(function (lists) {
                return lists;
            });
        }],
        listId: ['lists', '$stateParams', function($stateParams) {
            return ($stateParams.id === null) lists[0].id : listId;
        }],
        list: ['MailingListService', 'listId', function (MailingListService, listId) {
            return MailingListService.getMailingList(listId);
        }],
    },