【发布时间】:2015-08-07 09:59:50
【问题描述】:
我使用 Ionic 和 phonegap-nfc 编写了一个小型演示应用程序,它可以从 NFC 标签读取唯一 ID。
现在,我正在尝试创建一个显示以前读取事件的列表。一个事件应该被添加到这个列表中,一个标签被读取。
我有一个可以添加事件的列表。代码如下所示:
<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>
控制器如下所示:
.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);
};
})
所有这些都可以正常工作。您有一个按钮,可以打开一个模式,您可以在其中添加任务。按下按钮关闭模式,任务现在在列表中。
但是,我想在读取 NFC 标签时自动创建一个任务。我是使用 Angular 的初学者,所以我不知道如何将“ng-click”操作替换为与 phonegap-nfc 的 NFC 操作相对应的其他操作。
NFC 事件的控制器如下所示:
.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);
}
};
});
我该如何执行此操作?
【问题讨论】:
标签: javascript angularjs cordova ionic nfc