有没有更好的方法在状态
更改之前加载共享模型
?
....
....
.state('state1',{
...
resolve : {
student : function($stateParams,StudentModel,StudentService){
return StudentService.getStudentById($stateParams.studentId).then(
function(response){
return StudentModel.loadStudent(response.data);
}
);
}
}
}).....
.....
...
在我当前的应用程序中,我使用 resolve
属性加载所有共享数据模型
,以便之前不会执行任何 Controller /指令
StudentModel
已填充。但在这里,为了加载 StudentModel
,我在我的 state
中创建一个 已解析对象(本例中为学生)
我不会在其他地方使用。
在为特定状态
加载其他 Controller /指令
之前,有没有更好的方法来加载我的共享模型
?
[注意:我使用的是 Angular 1.4.2 版本]
更新:抱歉,我错过了上面代码中的 return 语句,这造成了一些困惑。我的问题不是让上面的代码工作。对于相同的行为有其他方法吗?因为在此设计中,我必须创建一个解析对象 (student
),该对象不会在任何地方使用,而 StudentModel
是在任何地方都使用的对象。
最佳答案
因此,您没有正确实现 Promise 函数。它应该返回等待 View 渲染的 promise 。
代码
student: function($stateParams, StudentModel, StudentService) {
return StudentService.getStudentById($stateParams.studentId).then(
function(response) {
return StudentModel.loadStudent(response.data);
}
);
}
除此之外,您能否详细说明问题中的以下行?
Is there any better way to load my shared models before other controllers/directives are loaded for a particular state?
关于javascript - 在 ui-router 状态下加载共享模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36198341/