繁体   English   中英

如何在Angularjs中访问promise中的作用域?

[英]How can I access a scope inside a promise in Angularjs?

这是我的控制器:

translateApp.controller('translateCtrl', function ($scope, $http) {
    $scope.spinner = false;
    $scope.talkButton = true;

    function redirectRecognizedTextOutput() {
        artyom.redirectRecognizedTextOutput((recognized, isFinal) => {
            if (isFinal) {
                // scope can't be read
                $scope.talkButton = true;
                $scope.spinner = false;
            } else {
                console.log("wait")
            }

        });
    }

    $scope.start = function () {
        // I set spinner and talkButton values and call the function
        $scope.spinner = true;
        $scope.talkButton = false;
        redirectRecognizedTextOutput()
    }
})

我的问题是$scope.spinner$scope.talkButton无法正常工作。 我尝试将其放在承诺之前,并且有效。 所以我尝试了类似

function redirectRecognizedTextOutput() {
    var vm = this
    artyom.redirectRecognizedTextOutput((recognized, isFinal) => {
      if (isFinal) {
        vm.spinner = false;
        vm.talkButton = true;
      } else {
        console.log("wait")
      }
    });
 }

但是仍然没有变化,也没有错误。 我做错了吗? 任何帮助将非常感激。

也许摘要循环可能已停止。 我认为使用scope.apply()可能会解决您的问题。

function redirectRecognizedTextOutput() {
    var vm = this
    artyom.redirectRecognizedTextOutput((recognized, isFinal) => {
      if (isFinal) {
        vm.spinner = false;
        vm.talkButton = true;
      } else {
        console.log("wait")
      }
      $scope.$apply()
    });
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM