从angularJS中的回调内部更新$ scope [英] Update $scope from inside callback in angularJS

查看:69
本文介绍了从angularJS中的回调内部更新$ scope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个控制器,我想从函数回调中更新 $ scope.progress 。我尝试使用 $ rootScope $ scope.apply()但我无法使用它。
我有什么遗漏吗?

I have this controller and I would like to update the $scope.progress from within the function callback. I tried using $rootScope and $scope.apply() but I can't get it to work. Is there something I'm missing?

progressupdate是事件返回的变量。
代码不完全是这样的。我在这里展示结构非常简单。

progressupdate is a variable returned by the event. the code is not exactly like this. I made it super simple here to show the structure.

app.controller('player', function($scope) {

    var show = function(url) {
        function(err, showOK) {
            if (err) {
                console.log(err);
            } else {
                showOK.on('listening', function(){
                     $scope.progress = progressupdate;
                });
            }
        });
    }

    show(url);

});

我是否在控制器内错误地运行了该功能?
我应该使用这样的东西吗?

Am I running that function incorrectly inside the controller? Should I use something like this?

 $scope.show = function(url)...etc


推荐答案

我没有看到 $在上面的脚本和progressupdate上应用函数。设置后尝试 $ apply ,或者在 $ apply 中设置:

I don't see the $apply function on above script, and progressupdate. Try $apply after set, or set it inside $apply :

showOk.on('listening', function(){
   $scope.$apply(function(){
      $scope.progress = progressupdate;
   });
});

showOk.on('listening', function(){
   $scope.progress = progressupdate;
   $scope.$apply();
});

建议使用第一种方法。

这篇关于从angularJS中的回调内部更新$ scope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆