读取 NFC 标签时调用 AngularJS 函数?
Call an AngularJS function when an NFC tag is read?
我使用 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);
}
};
});
我该如何执行此操作?
您的 nfcService 已经在侦听 tagDiscovered 事件,所以您只需要让 TaskCtrl 知道发生了什么事。为此,您有几个选择
- 使用事件/广播
一个。在 nfcService
$rootScope.$emit('tagFound', tag);
b。在 TaskCtrl
$rootScope.$on('tagFound', function(tag) {
newTask();
});
- 注册回调函数
一个。在 nfcService
中添加功能
var cb = null;
this.registerListener = function (callback){
this.cb = callback;
}
// inside addTagDiscoveredListener
...
cb.call(tag)
b。在 taskCtrl
// inject service as nfcService
nfcService.registerListener(myCallback)
function myCallback(tag) {
newTask();
}
进入新状态
// 在 nfcService 中
$state.go('/tasks/add');
我通常选择选项 2,但不要忘记注销以取消阻止垃圾收集。服务不应该关心状态,所以选项 3 不是最好的方式恕我直言
我使用 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);
}
};
});
我该如何执行此操作?
您的 nfcService 已经在侦听 tagDiscovered 事件,所以您只需要让 TaskCtrl 知道发生了什么事。为此,您有几个选择
- 使用事件/广播
一个。在 nfcService
$rootScope.$emit('tagFound', tag);
b。在 TaskCtrl
$rootScope.$on('tagFound', function(tag) {
newTask();
});
- 注册回调函数
一个。在 nfcService
中添加功能var cb = null;
this.registerListener = function (callback){
this.cb = callback;
}
// inside addTagDiscoveredListener
...
cb.call(tag)
b。在 taskCtrl
// inject service as nfcService
nfcService.registerListener(myCallback)
function myCallback(tag) {
newTask();
}
进入新状态
// 在 nfcService 中 $state.go('/tasks/add');
我通常选择选项 2,但不要忘记注销以取消阻止垃圾收集。服务不应该关心状态,所以选项 3 不是最好的方式恕我直言