在我的指令中,我进行一个API调用,获取数据并呈现它。当呈现正在进行时(即$digest正在运行),我想等待,然后再做一些事情。
这是我的实现,但不起作用:
// Assume I'm in the callback function of the $resource
function(response) {
// $scope.templates now needs to render
$scope.templates = (_.isEmpty(response.warning)) ? response.data : [];
var s = $scope.$watch('$$phase', function(v) {
if (_.isNull(v)) {
NowRunMyNextAwesomeAction()
// Silent the $watch
s();
}
});
}当我在console.log(v)中只记录$watch时,它会记录$diget,然后再不记录其他内容。为什么?我认为可能发生了其他事情,所以我尝试在1之后用一个$$phase记录$timeout,它显示了null。所以它绝对不是在运行
发布于 2014-06-21 00:53:32
因此,我找到了“一个”解决方案;我不知道这种良好实践是否有效,但它依赖于使用$timeout。我的印象是不推荐在Javascript中使用计时器?
无论如何,下面是如何做到这一点的:
// Assume I'm in the callback function of the $resource
function(response) {
// $scope.templates now needs to render
$scope.templates = (_.isEmpty(response.warning)) ? response.data : [];
$timeout(function(){
NowRunMyNextAwesomeAction();
});
}请注意,$timeout的延迟为零。有关更多信息,请请看这里。
https://stackoverflow.com/questions/24337222
复制相似问题