如何处理AngularJS服务中的回调
How to handle callback in services of AngularJS
我正在使用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次。
相关文章:
- 在AngularJS服务中使用$http回调
- 在$q服务中捕获拒绝而不触发成功回调
- $http服务未触发成功或错误回调
- 将web服务回调的结果绑定到Javascript中的调用函数
- AngularJS Protractor:正在等待服务回调
- 如何访问ng-init或ng-checked中服务回调函数中声明的$scope变量
- 如何在Angular中设置异步HTTP服务的回调,以便在不定义数组的情况下在控制器中填充数组
- 如何处理AngularJS服务中的回调
- 如何访问回调函数之外的异步angularjs服务数据
- 服务回调中的KendoUI网格服务器动态列绑定
- 回调函数不会在使用 $timeout 的服务中只调用一次
- 如何为方向服务编写回调函数
- 为什么无法在 Jquery/ajax 调用中获取对 RESTful 服务的成功回调
- 如何使用角度休息服务设置超时回调事件
- Angularjs:配置使用提供程序中服务的回调的好方法
- AngularJS:在回调中设置私有服务变量
- Angular JS从服务和Http回调方法返回数据
- 回调如何在 AngularJS 调用 REST 服务中工作
- 角服务在回调函数中部分可用
- 使用AngularJS中的promise管理来自控制器的服务回调