I've written a small demo application using Ionic and the phonegap-nfc which can read the unique ID from an NFC tag.
Now, I'm trying to create a list that shows previous read events. An event should be added to this list a tag is read.
I have a list which can have events added to it. The code looks like this:
<ion-view view-title="Usage History">
<ion-content>
<button class="button button-icon" ng-click="newTask()">
<i class="icon ion-compose"></i>
</button>
<ion-list>
<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="chat in chats" type="item-text-wrap" href="#/tab/tasks/{{task.id}}">
<img ng-src="{{task.pic}}">
<h2>{{task.name}}</h2>
<p>{{task.lastText}}</p>
<i class="icon ion-chevron-right icon-accessory"></i>
<ion-option-button class="button-assertive" ng-click="remove(task)">
Delete
</ion-option-button>
</ion-item>
</ion-list>
<script id="new-task.html" type="text/ng-template">
<div class="modal">
<!-- Modal header bar -->
<ion-header-bar class="bar-secondary">
<h1 class="title">New Task</h1>
<button class="button button-clear button-positive" ng-click="closeNewTask()">Cancel</button>
</ion-header-bar>
<!-- Modal content area -->
<ion-content>
<form ng-submit="createTask(task)">
<div class="list">
<label class="item item-input">
<input type="text" placeholder="What do you need to do?" ng-model="task.name">
</label>
</div>
<div class="padding">
<button type="submit" class="button button-block button-positive">Create Task</button>
</div>
</form>
</ion-content>
</div>
</script>
</ion-content>
</ion-view>
The controller looks like this:
.controller('TasksCtrl', function($scope, $ionicModal) {
$scope.tasks= [];
// Create and load the Modal
$ionicModal.fromTemplateUrl('new-task.html', function(modal) {
$scope.taskModal = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
// Called when the form is submitted
$scope.createTask = function(task) {
$scope.chats.push({
name: task.name
});
$scope.taskModal.hide();
task.name= "";
};
// Open our new task modal
$scope.newTask = function() {
$scope.taskModal.show();
};
// Close the new task modal
$scope.closeNewTask = function() {
$scope.taskModal.hide();
};
$scope.remove = function(task) {
tasks.remove(task);
};
})
All of this works fine. You have a button which opens up a modal where you can add a task. Pressing the button closes the modal and the task is now in the list.
However, I want to automatically create a task when a NFC tag is read. I'm a beginner using Angular, so I don't know how to replace the "ng-click" action with something else that corresponds to a NFC action with phonegap-nfc.
The controller for the NFC events looks like this:
.controller('MainController', function ($scope, nfcService) {
$scope.tag = nfcService.tag;
$scope.clear = function() {
nfcService.clearTag();
};
})
.factory('nfcService', function ($rootScope, $ionicPlatform) {
var tag = {};
$ionicPlatform.ready(function() {
nfc.addTagDiscoveredListener(function (nfcEvent) {
console.log(JSON.stringify(nfcEvent.tag, null, 4));
$rootScope.$apply(function(){
angular.copy(nfcEvent.tag, tag);
// if necessary $state.go('some-route')
});
}, function () {
console.log("Listening for tags.");
}, function (reason) {
alert("Error adding NFC Listener " + reason);
});
nfc.addMimeTypeListener('', function (nfcEvent) {
console.log(JSON.stringify(nfcEvent.tag, null, 4));
$rootScope.$apply(function(){
angular.copy(nfcEvent.tag, tag);
// if necessary $state.go('some-route')
});
});
});
return {
tag: tag,
clearTag: function () {
angular.copy({}, this.tag);
}
};
});
How can I perform this?
Your nfcService is already listening for the tagDiscovered event so you just need to let the TaskCtrl know that something happened. To do that you have a couple of options
a. In nfcService
$rootScope.$emit('tagFound', tag);
b. In TaskCtrl
$rootScope.$on('tagFound', function(tag) {
newTask();
});
a. Add function in nfcService
var cb = null;
this.registerListener = function (callback){
this.cb = callback;
}
// inside addTagDiscoveredListener
...
cb.call(tag)
b. in taskCtrl
// inject service as nfcService
nfcService.registerListener(myCallback)
function myCallback(tag) {
newTask();
}
Go to a new state
// in nfcService $state.go('/tasks/add');
I usually do option 2 but don't forget to unregister to unblock garbage collection. Services shouldn't care about states though so option 3 is not the best way imho
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With