javascript - AngularJS 使用 ng-style 设置宽度

标签 javascript css angularjs

我想在触发来自 S3 的事件时动态调整 div 的大小。

在下面的代码中,我发送了 progress变量到 updateProgress用作参数。此功能在我的 Angular Controller 的最高范围内。

s3.upload(params).on('httpUploadProgress', function(evt) {
        var progress;
        progress = Math.floor(evt.loaded * 100 / evt.total);
        updateProgress(progress);
    })
    .send(function(err, res, data) {
        if (err) {
            growl.error("We're having trouble uploading your image. Please try again!", {title: 'Whoops!', ttl: 5000});
            console.log('Error uploading' + err);
        } else {
            imgLocation = res.Location;
            postToFirebase(imgLocation);
        }
    });

这是我的 updateProgress 函数

function updateProgress(percent) {
    $scope.progressBar = {
        'width': percent + '%'
    }
}

HTML

<div ng-style="progressBar" class="shot__image--post__progress"></div>

我可以成功地控制台记录 AWS 事件和进度值。但是,当我上传图片时,宽度永远不会改变。

最佳答案

Phil 在此线程的评论中的回答。

Assuming the s3 library is not part of or privy to the Angular digest cycle, you need to wrap the changes to $scope properties in $scope.$apply or $scope.$applyAsync. See docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply. User Raghu answered this earlier but was downvoted for some reason

解决方案是在 updateProgress fn 中简单地调用 $scope.$apply()

关于javascript - AngularJS 使用 ng-style 设置宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39071008/

相关文章:

javascript - 以下javascript的解释

jquery - 我的代码有问题,我有一个 "-",必须在每个 Enter 键上插入

css - 制作一个 Sass mixin,将部分 CSS 写入不同的 CSS 文件

javascript - 如何清除angularjs中下拉列表的值

javascript - Angular JS 和 Bootstrap Modal 的数据绑定(bind)问题

javascript - 如何使用 Webpack 运行 Angular 2 AOT

javascript - 在下一个 flexslider 幻灯片中滑动后,css 动画不起作用

css - 我不明白为什么我的下拉菜单没有隐藏

javascript - 如何在angularjs中将范围变量设置为空

javascript - Angular JS SSL客户端身份验证