gpt4 book ai didi

angularjs - 为什么我的 AngularJS 绑定(bind)并不总是正确更新?

转载 作者:行者123 更新时间:2023-12-01 03:39:11 26 4
gpt4 key购买 nike

我正在使用 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>

在我的 Controller 上:
$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 时 DOM 没有更新。在 AngularJS 摘要中更新。

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

最佳答案

很有可能是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);
}
});
}
};

关于angularjs - 为什么我的 AngularJS 绑定(bind)并不总是正确更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32075605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com