ANGULARJS-$ scope在我第一次调用异步方法时不适用 [英] ANGULARJS - $scope does not apply the first time I call my async method
问题描述
我的angularjs控制器有问题.就像这样
I have a problem with my angularjs controller. It goes like this
我在单击按钮时触发了一个异步方法,该方法显示一个带有输入文本的对话框,一旦关闭对话框,该文本即被添加到作用域数组,但是,我第一次执行异步方法时,视图不能反映范围的更改,如果我再次单击并再次调用该函数,则可以看到视图中的所有更改(包括我所做的但没有立即看到的第一个更改)
I am triggering an async method on button click, this method shows a dialog with an input text, once you close the dialog, that text is added to a scope array, However, the first time I execute the async method the view does not reflect the changes in the scope, if I click a second time and the function gets called again then I can see all the changes in my view (including the first change I made and didn't see immediately)
这是我的示例代码
var afterUpload = function (result) {
vm.testme = result;
vm.pdfs.list.push({ systemFilename: 'test', note: 'test' });
$scope.pdfs.push({ systemFilename: 'test', note: 'test' });
}
$scope.upload = function (files) {
var modalOptions = {
closeButtonText: 'Ok',
actionButtonText: 'Accept',
headerText: 'File upload',
bodyText: 'Please type the name of the file',
modalTemplate: '/MiniSpa/app/templates/modal/file-modal.html'
};
modalService.showModal({}, modalOptions).then(function(result) {
$scope.$evalAsync(function () { afterUpload(result); });
}
有什么想法吗?
推荐答案
将代码放入$ timeout内,以打开对话框.它应该可以解决问题.
Put the code inside $timeout which opens the dialog. It should resolve the issue.
$scope.upload = function() {
...
$timeout(function() {
// put code here which opens dialog
}, 200);
}
这篇关于ANGULARJS-$ scope在我第一次调用异步方法时不适用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!