我有一个类似于主控制器的东西,可以在范围内设置一些东西,所以内部控制器可以使用它。
该设置工作是异步的,所以我将它包装在一个承诺中,但它不会执行它的回调,除非它已经解决(我尝试设置一个断点,如果我等待足够多,它实际上会运行then
回调)。
这是一个用超时而不是网络请求重现我的问题的小提琴:http: //jsfiddle.net/LMv8v/1/
HTML
<div ng-app>
<div ng-controller="configController">
<div ng-controller="testC">
{{test}}
{{foo}}
</div>
</div>
</div>
Javascript
function configController ($scope, $q) {
var deferred = $q.defer();
$scope.config = deferred.promise;
setTimeout(function() {
console.log('timeout');
deferred.resolve({
'foo' : 'baz'
});
}, 1000);
};
function testC($scope) {
$scope.test = 'I am working, uh?';
$scope.config.then(function(config) {
console.log('then...');
$scope.$apply(function() {
$scope.foo = config.foo;
});
});
};
它显示“超时”,但不显示“然后... ”消息。
(我知道这将更适合服务,但我已经有很多嵌套范围的代码,我想在开始重构之前让它工作)