Im new with Angular and have...let´s say a few starter problems. I have made an small app with phonegap, onsen ui and angluarjs using the nfc-plugin. All i want to do is reading a tag-id from a nfc-tag. Everything works fine when i place my whole code within the controller.
This looks like that:
app.controller('Page3Ctrl', function($scope, Data) {
$scope.item = Data.selectedItem.title;
$scope.save = function() {
Data.selectedItem.title = $scope.item;
$scope.ons.navigator.popPage();
};
// Get the ID Number from the tag
$scope.onNfc = function(nfcEvent) {
var tag = nfcEvent.tag;
var taglesen = nfc.bytesToHexString(tag.id);
$scope.$apply(function() {
$scope.item = taglesen;
});
};
nfc.addTagDiscoveredListener(
$scope.onNfc, // tag successfully scanned --> call $scope.onNfc
function (status) { // listener successfully initialized
$scope.nfcok = "NFC Reader ist ready";
},
function (error) { // listener fails to initialize
$scope.nfcok = "NFC Reader ist nicht ready";
}
);
});
<ons-page class="center">
<div ng-controller="Page3Ctrl">
<ons-text-input ng-model="item" style="margin:10px;"></ons-text-input><br>
<ons-text-input ng-model="nfcok" style="margin:10px;"></ons-text-input><br>
<ons-button ng-click="save()">Save</ons-button>
<ons-button ng-click="onNfc()">Scan NFC</ons-button>
</div>
</ons-page>
But now want to bring the NFC-Reading part to a separte file called services.js and place it into a factory. A there is my problem how to do this.
I have tried this but unfortunately it doesn't work. Mybe i just need a small tipp or i am on a completly wrong way?????:
myApp.factory('onNfc', function() {
this.getNfc = function(nfcEvent) {
var tag = nfcEvent.tag;
var taglesen = nfc.bytesToHexString(tag.id);
return taglesen;
}
nfc.addTagDiscoveredListener(
this.getNfc(), // 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('Page3Ctrl', function($scope, Data, onNfc) {
$scope.item = Data.selectedItem.title;
$scope.save = function() {
Data.selectedItem.title = $scope.item;
$scope.ons.navigator.popPage();
};
$scope.$apply(function() {
$scope.item = onNfc.getNfc();
});
});
I am grateful for any help
Best regards,