Javascript 在ui路由器状态下加载共享模型
在Javascript 在ui路由器状态下加载共享模型,javascript,angularjs,angular-ui-router,angular-promise,Javascript,Angularjs,Angular Ui Router,Angular Promise,在状态更改之前,是否有更好的方法加载共享模型 .... .... .state('state1',{ ... resolve : { student : function($stateParams,StudentModel,StudentService){ return StudentService.getStudentById($stateParams.studentId).then(
状态更改之前,是否有更好的方法加载共享模型
....
....
.state('state1',{
...
resolve : {
student : function($stateParams,StudentModel,StudentService){
return StudentService.getStudentById($stateParams.studentId).then(
function(response){
return StudentModel.loadStudent(response.data);
}
);
}
}
}).....
.....
...
在我当前的应用程序中,我使用resolve
属性加载我所有的共享数据模型
,这样在填充StudentModel
之前就不会执行控制器/指令
。但是在这里,为了加载StudentModel
,我在状态下创建了一个解析对象(本例中为student)
,我不会在其他任何地方使用它
在为特定的状态加载其他控制器/指令之前,是否有更好的方法加载我的共享模型
[注:我使用的是angular 1.4.2版本]
更新:很抱歉,我错过了上面代码中的return语句,这造成了一些混乱。我的问题不是要让上述代码正常工作。对于同样的行为,有没有其他方法?因为在这个设计中,我必须创建一个resolve对象(student
),它不会在任何地方使用,而StudentModel
则是到处使用的对象。因此,您没有正确实现promise函数。它应该返回承诺等待视图渲染
代码
student: function($stateParams, StudentModel, StudentService) {
return StudentService.getStudentById($stateParams.studentId).then(
function(response) {
return StudentModel.loadStudent(response.data);
}
);
}
除此之外,你们能详细说明问题的下一行吗
在为特定状态加载其他控制器/指令之前,是否有更好的方法加载我的共享模型
您需要在解析中返回$PROMITE,以便在创建控制器之前加载数据
resolve : {
student : function($stateParams,StudentModel,StudentService){
StudentService.getStudentById($stateParams.studentId).$promise;
}
}
然后在控制器中,需要从参数中获取学员
var StudentController = function(student, $scope) {
....
}
app.controller("StudentController", ["student", "$scope", StudentController]);