angularjs中的'viewModel'是什么,它如何按顺序工作?

问题描述 投票:0回答:1

我想知道定义状态时的顺序。我可以解释一下下面的代码

    $stateProvider.state("clients.index", {
        url: "",
        templateUrl: "/client/index",
        controller: "clientIndexController",
        resolve: {
            viewModel: ["clientService", function (clientService) {
                return clientService.getAllClients();
            }]
        }
    });
.factory("clientService", ["$http", function ($http) {
    return {
        getAllClients: function () {
            return $http({
                method: 'GET',
                url: '/api/client/list',
                headers: {
                    'Content-type': 'application/json'
                }
            });
        }
    }

}])

resolve是在加载控制器和templateURL之前必须发生的承诺。但是viewModel呢?那是保留字吗?我转到clientService,然后获取客户端列表,然后将该客户端列表分配给viewModel

angularjs angular-ui-router
1个回答
0
投票

viewModel只是一个属性名称。一个可以使用任何名称。使用更有意义的名称(例如allClients)会更明智。

路由器状态的resolve属性是应注入控制器的依赖关系图。如果这些依赖关系中的任何一个是应许的,路由器将等待所有这些依赖关系被解决或被拒绝。如果所有的诺言都已成功解决,则将注入已解决的诺言的值。

一个可以为resolve对象定义多个属性:

$stateProvider.state("clients.index", {
    url: "",
    templateUrl: "/client/index",
    controller: "clientIndexController",
    resolve: {
        allClients: ["clientService", function (clientService) {
            return clientService.getAllClients();
        }],
        otherData: function(otherService) {
            return otherService.getOtherData();
        }
    }
});

在这种情况下,两种服务都需要在路由器实例化控制器和模板之前成功返回数据。

© www.soinside.com 2019 - 2024. All rights reserved.