在SO上有以下几个类似的问题。我找到的答案已经过时,错误或者没有解决我的问题。
以下摘要被删除至此问题所必需的内容。
查看:
<div ng-app="main">
<!-- some stuff -->
<ng-include src="getPartial(case.status)"></ng-include>
</div>
所包含部分的相关部分:
<div class="controls">
<label class="checkbox" for="iterationDiarySurvey">
<a ng-show="surveyDiaryMailSent == 0" ng-click="sendMail('diaryLinks')">
Send Mail
</a>
<span ng-show="surveyDiaryMailSent == 1" class="badge">Mail sent</span>
</label>
</div>
点击时调用的 sendMail()
函数:
$scope.sendMail = function(type) {
<!-- snip -->
} else if (type == 'diaryLinks') {
Mail.create({some data}).then(function() {
$scope.surveyDiaryMailSent = 1;
});
}
};
成功完成承诺后的预期行为将是$scope.surveyDiaryMailSent
设置为1,<a>
元素消失,<span>
元素示出。
但这不会发生。仅在手动刷新浏览器之后。
我在其他地方做同样(非常相似)的事情,它总是很好。我无法在这种特殊情况下更新视图。
什么没有帮助:
$apply
中。导致通常的错误导致“消化已经在进行中”。$apply
,原因相同答案 0 :(得分:1)
您还没有显示Mail
课程的代码,因此我无法确定Mail.send()
返回的承诺是否为Angular承诺。但是,您可以使用$q.when()
打包调用,以确保Angular在解析promise时知道:
function MyController($scope, Mail, $q) {
/* ... */
$scope.sendMail = function(type) {
/* ... */
$q.when(Mail.create({some data})).then(function(result) {
// result will be the resolved value
$scope.surveyDiaryMailSent = 1;
});
}
}