ANGULARJS - $scope第一次调用异步方法时不适用

ANGULARJS - $scope does not apply the first time I call my async method

本文关键字:异步方法 不适用 调用 第一次 scope ANGULARJS      更新时间:2023-09-26

我的angularjs控制器有问题。它是这样的

我在单击按钮时触发了一个异步方法,此方法显示一个带有输入文本的对话框,一旦关闭对话框,该文本就会添加到作用域数组中,但是,当我第一次执行异步方法时,视图不会反映作用域中的更改,如果我第二次单击并且函数再次被调用,那么我可以看到视图中的所有更改(包括第一个我所做的更改没有立即看到)

这是我的示例代码

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。它应该可以解决问题。

例:

$scope.upload = function() {
 ...
 $timeout(function() {
     // put code here which opens dialog
 }, 200);
}