$scope没有在第三方API异步调用后对视图应用更改

$scope Doesn't Apply Change To View After Async Call to Third Party API

本文关键字:视图 应用 调用 异步 scope API 第三方      更新时间:2023-09-26

我正在对语音识别API进行异步调用,并尝试将结果存储在$scope变量中,但视图没有改变,即使结果显示在console.log中也很好。我尝试使用$scope.$apply()$scope.applyAsync();建议在这些线程-这个和这个,不幸的是没有效果

控制器是这样的:

myApp.controller('audiotest', function($scope)
                    {
    $scope.recognize = function(){
        speech.main('test.wav', function(err, res){
        $scope.myResult = res.results[0].alternatives[0].transcript;
        console.log($scope.myResult); // <- this logs correctly
        $scope.$applyAsync(); // also tried with $scope.$apply();
        });
    };
 };

我不认为这个标记是相关的,但无论如何,这里是:

<div class="row" ng-controller="audiotest">
<div class="row">
   <button type="button" ng-click="recognize()"> Attempt recognition </button>
<p> Result is {{myResult}} </p>
</div>
</div>

提前感谢您的意见和建议。

不确定,但尝试在声明识别函数之前初始化作用域上的myResult。