如何访问从异步回调范围 [英] How to access scope from async callback

查看:132
本文介绍了如何访问从异步回调范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它必须似乎真的简单,但我不知道如何从一个异步函数访问$范围。
用下面的我不会UDPATE $ scope.result直到我手动重新加载标签。

It must seems really simple but I have no idea how to access $scope from an async function. With the following I won't udpate $scope.result until I reload the tab manually.

.controller('DashCtrl', function ($scope, Camera) {
    $scope.getBarcode = function () {
        cordova.plugins.barcodeScanner.scan(function (result) {
            $scope.result = result.text;
        }, function (error) {
            //alert("Scanning failed: " + error);
        });
    };
}

你能不能让我知道如何进行呢?

Could you let me know how to proceed ?

谢谢!

推荐答案

您需要触发消化周期在这种情况下手动,因为 cordova.plugins.bar codeScanner.scan 不是角方法,以便框架有不知道这code应该更新绑定。在这种情况下,你需要踢与 $范围消化自己的$适用()

You need to trigger digest cycle manually in this case, since cordova.plugins.barcodeScanner.scan is not Angular method so framework has no idea that this code should update bindings. In this case you need to kick of digest yourself with $scope.$apply():

$scope.getBarcode = function () {
    cordova.plugins.barcodeScanner.scan(function (result) {
        $scope.result = result.text;
        $scope.$apply();
    }, function (error) {
        //alert("Scanning failed: " + error);
    });
};

这篇关于如何访问从异步回调范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆