【问题标题】:Why is my AngularJS binding not always updating properly?为什么我的 AngularJS 绑定并不总是正确更新?
【发布时间】:2015-08-18 14:44:00
【问题描述】:

我正在使用 Danial Farid's ng-file-upload plugin 创建一个与 AWS S3 一起使用的简单图像上传页面。它具有一个简单的按钮来上传新图像和图像列表。像这样:

<ul>
    <li ng-repeat="image in product.images">
        <a class="thumbnail" href="{{ image }}" target="_blank">
          <img ng-src="{{ image }}" alt="image">
        </a>
    </li>
    <li>
        <div class="btn-image-upload" 
            ngf-select 
            ngf-change="upload($files)"
            ngf-accept="'image/*'"></div>
    </li>
</ul>

在我的控制器上:

$scope.upload = function (files) {

    var aws_policy, aws_policy_signature;

    if (files && files.length) {
        $http
            .get(API_URL+'/helper/aws_policy')
            .then(function(data) {
                aws_policy = data.data.policy;
                aws_policy_signature = data.data.signature;
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    var key = generate_aws_key() + '.' + file.name.split('.').pop();
                    var base_url = 'https://.../';

                    Upload
                    .upload({ ... })
                    .success(function( data, status, headers, config ) {

                        console.log($scope.product);
                        $scope.product.images.push( base_url + key );
                        console.log($scope.product);

                    });
                }
            });
    }
};

文件正在正确上传(我从 AWS 得到了一个不错的 204 Success)并且两个 console.log($scope.product) 被调用显示了适当的结果(第二个具有 images 数组中的项目)。

事情就是这样。这个宝贝在我的开发机器上完美运行,但有时在登台或生产服务器上,当$scope.product.images 更新时,图像列表没有更新。有时,我的意思是它会执行 1/3 次。

总结

有时,仅在我的生产服务器上,当 $scope.product.images 在 AngularJS 摘要中更新时,DOM 不会更新。

我的编程经验告诉我,有时在这类事情中不是一个可接受的概念,它必须与当这个问题出现时总是发生的事情有关,但我进行了广泛的调试,未能找到真正的原因。想法?

【问题讨论】:

  • 您是否尝试过将成功代码封装在 $timeout() 中?

标签: angularjs angular-file-upload


【解决方案1】:

很有可能是looping within asynchronous callbacks引起的:

你必须冻结i的值:

$scope.upload = function (files) {

    var aws_policy, aws_policy_signature;

    function upload(index) {
        var file = files[index];
        var key = generate_aws_key() + '.' + file.name.split('.').pop();
        var base_url = 'https://.../';

        Upload
        .upload({ ... })
        .success(function( data, status, headers, config ) {

            console.log($scope.product);
            $scope.product.images.push( base_url + key );
            console.log($scope.product);

        });
    }

    if (files && files.length) {
        $http
            .get(API_URL+'/helper/aws_policy')
            .then(function(data) {
                aws_policy = data.data.policy;
                aws_policy_signature = data.data.signature;
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    upload(i);
                }
            });
    }
};

【讨论】:

  • 哇,这对我来说是新的。理论上和我写的代码是一样的!
猜你喜欢
  • 2014-12-02
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2020-09-12
相关资源
最近更新 更多