从angularJS中的回调内部更新$ scope

fmt*_*olo 23 javascript scope angularjs angularjs-scope

我有这个控制器,我想$scope.progress在函数回调中更新from.我试着用$rootScope$scope.apply(),但我不能得到它的工作.有什么我想念的吗?

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

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);

});
Run Code Online (Sandbox Code Playgroud)

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

 $scope.show = function(url)...etc
Run Code Online (Sandbox Code Playgroud)

Iqb*_*uzi 35

我没有$apply在上面的脚本和progressupdate上看到该功能.尝试$apply设置后,或在里面设置$apply:

showOk.on('listening', function(){
   $scope.$apply(function(){
      $scope.progress = progressupdate;
   });
});
Run Code Online (Sandbox Code Playgroud)

要么

showOk.on('listening', function(){
   $scope.progress = progressupdate;
   $scope.$apply();
});
Run Code Online (Sandbox Code Playgroud)

建议使用第一种方法.