如何处理AngularJS服务中的回调

How to handle callback in services of AngularJS

本文关键字:服务 回调 AngularJS 何处理 处理      更新时间:2024-01-29

我正在使用Phonegap插件读取NFC标签的ID,并希望在.factory 中使用此插件

那么它的制造:

在我的.factory中,我调用命令"nfc.addTagDiscoveredListener",该命令为nfc标签注册一个事件侦听器。成功后,他将函数称为holeNfc,并应该引发一个nfcevent。

事实上,我的函数holeNfc被调用了,但nfcEvent不会到达。因此,行-->标记=nfcEvent.tag;不会工作,因为他没有得到nfcEvent。

代码:

app.factory('leseNfc', function($rootScope) {
        // Items exposed to other modules
        
        return {
           initNfc: initNfc
        };
        
      
       function initNfc() {
            var tag = '';
            var taglesen = '';
            function holeNfc(nfcEvent) {
                tag = nfcEvent.tag;
                taglesen = nfc.bytesToHexString(tag.id);
            }
            nfc.addTagDiscoveredListener(
                 holeNfc(),             // tag successfully scanned
                 function (status) {    // listener successfully initialized
                    msg = "NFC Reader ist ready";
                    //return msg;
                 },
                 function (error) {     // listener fails to initialize
                    msg = "NFC Reader ist nicht ready";
                    //return msg;
                 }
            );
            return taglesen;
        }
    });

我也在我的控制器中尝试了同样的方法,它在那里工作没有问题:

app.controller('Page3Ctrl', function($scope, $rootScope, Data, leseNfc, Calc) {
        $scope.item = Data.selectedItem.title;
        
        $scope.save = function() {
            Data.selectedItem.title = $scope.item;
            $scope.ons.navigator.popPage();
        };
        $scope.readNfc = function(nfcEvent) {
            
            var tag = nfcEvent.tag;
            var taglesen = nfc.bytesToHexString(tag.id);
            $scope.$apply(function() {
                $scope.nfcvalue = taglesen;
            });
        };
        nfc.addTagDiscoveredListener(
             $scope.onNfc,             // tag successfully scanned
             function (status) {    // listener successfully initialized
                $scope.nfcok = "NFC Reader ist ready";
             },
             function (error) {     // listener fails to initialize
                $scope.nfcok = "NFC Reader ist nicht ready";
             }
        );
    });

我的控制器:

app.controller('NFCCtrl', function($scope, $rootScope, Data, Calc, onNfc, leseNfc) {
        $scope.item = Data.selectedItem.title;
        
        $scope.save = function() {
            Data.selectedItem.title = $scope.item;
            $scope.ons.navigator.popPage();
        };
                   
        $scope.readnfc = function() {
            $scope.nfcvalue = leseNfc.initNfc();
        };
    });
<ons-page class="center">
    <div ng-controller="NFCCtrl">
        <ons-text-input ng-model="item" style="margin:10px;"></ons-text-input><br>
        <ons-text-input ng-model="nfcvalue" style="margin:10px;"></ons-text-input><br>
        <ons-button ng-click="save()">Save</ons-button>
        <ons-button ng-click="readnfc()">Nfc</ons-button>
    </div>
</ons-page>

似乎是在第一个代码段上调用函数。实际上,您应该将其作为参数传递。

只需将holeNfc()更改为holeNfc

 nfc.addTagDiscoveredListener(
             holeNfc,             // tag successfully scanned
             function (status) {    // listener successfully initialized
                msg = "NFC Reader ist ready";
                //return msg;
             },
             function (error) {     // listener fails to initialize
                msg = "NFC Reader ist nicht ready";
                //return msg;
             }
        );

EDIT更换工厂这样会更好。

app.factory('leseNfc', function($rootScope) {
    // Items exposed to other modules
    return {
       initNfc: initNfc
    };

   function initNfc(callback) {

        function holeNfc(nfcEvent) {
            var tag = nfcEvent.tag;
            var taglesen = nfc.bytesToHexString(tag.id);
            callback(taglesen);

        }
        nfc.addTagDiscoveredListener(
             holeNfc,             // tag successfully scanned
             function (status) {    // listener successfully initialized
                msg = "NFC Reader ist ready";
                //return msg;
             },
             function (error) {     // listener fails to initialize
                msg = "NFC Reader ist nicht ready";
                //return msg;
             }
        );
    }
});

在你的控制器中,你可以做一些类似于你以前在读取值上所做的事情:

initNfc(function(taglesen){
     $scope.$apply(function() {
            $scope.nfcvalue = taglesen;
        });
);

不同的是,这个插件在你注册后似乎一直在调用你的代码,所以你真的应该只调用initNfc一次,因为它甚至可以在每次读到东西时调用你的码2到3次。