如何处理AngularJS服务中的回调

时间:2015-02-19 11:36:28

标签: javascript angularjs cordova

我正在使用Phonegap-Plugin来读取NFC标签的ID,并希望在.factory中使用它。

它是什么:

在我的.factory中,我调用了命令“nfc.addTagDiscoveredListener”,它注册了NFC-Tags的事件监听器。成功后,他调用函数holeNfc并应该触发一个nfcevent。

实际上我的函数holeNfc被调用但是nfcEvent不会到达。 进一步的结果是线 - > tag = 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>

1 个答案:

答案 0 :(得分:1)

您似乎在第一个代码段上调用了您的函数。实际上你应该把它作为参数传递。

只需从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;
             }
        );

编辑改变工厂这样的事情会更好。

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次。