Angular UI路由器相关组件解析

时间:2016-10-20 14:50:37

标签: javascript angularjs angular-ui-router

在UI路由器示例应用程序中,有一个示例,其中一个解析对象在下一个解析中使用。 https://github.com/ui-router/sample-app-ng1/blob/master/app/mymessages/mymessages.states.js#L76

resolve: {
    // Fetch the current folder from the Folders service, using the folderId parameter
    folder: (Folders, $stateParams) => Folders.get($stateParams.folderId),

    // The resolved folder object (from the resolve above) is injected into this resolve
    // The list of message for the folder are fetched from the Messages service
    messages: (Messages, folder) => Messages.byFolder(folder)
  }

我在ES5 javascript中尝试了下面的代码,但是我收到了一个错误:

  

"Cannot read property 'propertyName' of undefined"

resolve: {
 viewModel: ['viewModelService', function (viewModelService) {
        return viewModelService.get().then(function (response) {
            return response.data;
        });
    }],
    anotherViewModel: this.viewModel.propertyName
}

1 个答案:

答案 0 :(得分:3)

这应该有效:

resolve: {
    viewModel: ['viewModelService', function (viewModelService) {
            return viewModelService.get().then(function (response) {
                return response.data;
            });
        }
    ],
    anotherViewModel: ['viewModel', function (viewModel) {
            viewModel.propertyName
        }
    ]
}