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

What is 'viewModel' in angularjs and how it works in order?

我想知道定义 state 时的顺序是如何工作的。 我可以解释看到下面的代码

    $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 是加载控制器和模板URL 之前必须发生的承诺。但是 viewModel 呢?那是保留字吗?我转到 clientService 然后获取客户列表,然后将该客户列表分配给 viewModel?

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

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

一个人可以为 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();
        }
    }
});

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