0

So this is my controller:

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()
    }
})

And my problem is that the $scope.spinner and $scope.talkButton doesn't work as expected. I tried putting it before the promise and it works. So I tried something like

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

But still no changes and no errors. Am I doing it wrong? Any help would be much appreciated.

3
  • 1
    this != $scope? Where exactly do you expect the angular scope to come from (It's not a global variable)? Commented Jun 11, 2017 at 12:48
  • @Bergi I updated my question. Thanks for your comment. Commented Jun 11, 2017 at 13:01
  • 1
    Thanks for the update, but I can't believe that "scope can't be read". Is there an error message, what exactly does not work as expected? Btw, I don't see any promise in your code, so I don't know what "putting it before the promise" means. Commented Jun 11, 2017 at 14:22

1 Answer 1

3

maybe the digest cycle might be stopped. i think using scope.apply() might fix your problem.

function redirectRecognizedTextOutput() {
    var vm = this
    artyom.redirectRecognizedTextOutput((recognized, isFinal) => {
      if (isFinal) {
        vm.spinner = false;
        vm.talkButton = true;
      } else {
        console.log("wait")
      }
      $scope.$apply()
    });
 }
Sign up to request clarification or add additional context in comments.

1 Comment

because the promise might cause to stop the digest cycle(which enable the two-way binding between view and model in angular.). in order to manually start it ,there might be slight delay

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.