How do I change an AngularJS variable I declared in $scope using annyang speech commands?

╄→гoц情女王★ 提交于 2019-12-24 10:54:07

问题


I have a simple speech recognition app that starts and pauses a video based on speech commands. My problem is that a variable that I declare in $scope does not change when I give the pause command. The intent is for the number in the textarea to change when I pause the video using the "pause" speech command.

Link to fiddle

var myApp = angular.module('myApp', ['vjs.video']);

myApp.controller("myCtrl", function MyCtrl($scope) {
  $scope.playtime = 0; 

  $scope.$on('vjsVideoReady', function(e, data) {
    $scope.vid = data.player;

    $scope.commands = {
      'play': function() {
        $scope.vid.play();
      },
      'pause': function() {
        $scope.vid.pause();
        $scope.playtime = 10;
      }
    };

    $scope.ay = annyang;
    $scope.ay.addCommands($scope.commands);
    $scope.ay.debug();
    $scope.ay.start();

  });

});

Thank you very much for your help.


回答1:


just simply put $scope.$digest in your pause callback:

'pause': function() {
    $scope.vid.pause();
    $scope.playtime = 10;
    $scope.$digest();
}

so angular will know something has changed when you are tried to change $scope variable outside angular's working area.

hope that helps



来源:https://stackoverflow.com/questions/46028088/how-do-i-change-an-angularjs-variable-i-declared-in-scope-using-annyang-speech

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!