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

标签 javascript angularjs asynchronous angularjs-scope

我的 angularjs Controller 有问题。事情是这样的

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

这是我的示例代码

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);
}

关于javascript - ANGULARJS - $scope 在我第一次调用异步方法时不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38401302/

相关文章:

javascript - 单击显示文本字段(输入)

javascript - Node/Angular 应用程序未捕获语法错误 : Unexpected token <

android - 与 kotlin 异步运行代码的最佳方式

javascript格式化程序

javascript - 如何在for循环中通过引用获取变量?

javascript - 模拟 Angular JS Controller Jasmine

javascript - ngRepeat 中的 equals 有什么作用?

java - 从解析的 XML 文件创建表时发生 Android 错误

javascript - 如何使用异步代码以正确的顺序记录响应

javascript - 没有选择器的 JQuery?