【问题标题】:ANGULARJS - $scope does not apply the first time I call my async methodANGULARJS - $scope 在我第一次调用我的异步方法时不适用
【发布时间】:2016-07-15 16:46:20
【问题描述】:

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

有什么想法吗?

【问题讨论】:

    标签: javascript angularjs asynchronous angularjs-scope


    【解决方案1】:

    将代码放入打开对话框的 $timeout 中。它应该可以解决问题。

    示例:

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

    【讨论】:

    • 嗨 Suneet,这没有用,但我在同事建议后删除了对话框,尝试隔离问题,结果是对话框,没有对话框,它所有工作文件。我现在正在寻找一种新的对话方式。这是我以前使用的那个,它似乎弄乱了我的 $scope weblogs.asp.net/dwahlin/building-an-angularjs-modal-service
    猜你喜欢
    • 2014-02-14
    • 2021-12-18
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多