在ui路由器的深入指南中,它在嵌套状态章节中有一个注释,其中表示
注意:如果你,必须将解决密钥注入子状态 想要在实例化之前等待承诺得到解决 孩子。
我正在使用以下示例,在我看来,子状态总是等待父状态解析密钥的承诺解决,无论我是否在状态中注入它。
.state('contacts', {
templateUrl: 'contacts.html',
resolve: {
// a key that resolves in a second
resA: function($q, $timeout) {
var deferred = $q.defer();
$timeout(function() {
deferred.resolve('promise resolved');
}, 1000);
return deferred.promise;
}
},
controller: function($scope, resA) {
console.log(resA);
}
})
.state('contacts.list', {
templateUrl: 'contacts.list.html',
// here i do not inject the resolved key from the
// parent state but the ctrl still waits 1 second
// before it executes
controller: function() {
console.log('resolved');
}
});
因此,我无法理解官方指南中的说明。
答案 0 :(得分:3)
对于控制器 - 是的,他们总是等待他们的州(或父州)解决。但是,当您的子状态 解决对象时,可能会出现这种情况,其中某些逻辑取决于父母的解决方案 - 在这种情况下,您应该明确提供此依赖关系; 例如:
.state('contacts', {
templateUrl: 'contacts.html',
resolve: {
// a key that resolves in a second
resA: function($q, $timeout) {
var deferred = $q.defer();
$timeout(function() {
console.log('I am second, although I am a parent');
deferred.resolve({id: 'initial'});
})
return deferred.promise;
}
},
controller: function($scope, resA) {
console.log(resA);
}
})
.state('contacts.list', {
templateUrl: 'contacts.list.html',
resolve: {
// if you do not provide `resA` dependency here
// your child's `resB` will be the first to resolve
resB: function() {
console.log('I am first');
return 'child promise resolved';
}
},
controller: function() {
console.log('resolved');
}
});