私は初心者angularjs
です。私のNFCプロジェクトでは、変更するpatientId
に基づいてサーバーデータからGETできるようにしたいと考えています。
しかし、新しいNFCタグをスキャンするたびにpatientId
が変更されることがわかりましたが、私は$watch
が正しく実行されているのを見ることができません。
var nfc = angular.module('NfcCtrl', ['PatientRecordsService'])
nfc.controller('NfcCtrl', function($scope, NfcService, PatientRecordsService) {
$scope.tag = NfcService.tag;
$scope.patientId = NfcService.patientId
$scope.$watch(function() {
return NfcService.patientId;
}, function() {
console.log("Inside watch");
PatientRecordsService.getPatientRecords(NfcService.patientId)
.then(
function(response) {
$scope.patientRecords = response
},
function(httpError) {
throw httpError.status + " : " +
httpError.data;
});
}, true);
$scope.clear = function() {
NfcService.clearTag();
};
});
nfc.factory('NfcService', function($rootScope, $ionicPlatform, $filter) {
var tag = {};
var patientId = {};
$ionicPlatform.ready(function() {
nfc.addNdefListener(function(nfcEvent) {
console.log(JSON.stringify(nfcEvent.tag, null, 4));
$rootScope.$apply(function(){
angular.copy(nfcEvent.tag, tag);
patientId = $filter('decodePayload')(tag.ndefMessage[0]);
});
console.log("PatientId: ", patientId);
}, function() {
console.log("Listening for NDEF Tags.");
}, function(reason) {
alert("Error adding NFC Listener " + reason);
});
});
return {
tag: tag,
patientId: patientId,
clearTag: function() {
angular.copy({}, this.tag);
}
};
});
私がここで紛失していることがわかりません - 私を啓発してください! raksliceの勧告パー
更新 、私は工場内の私のデータを保持するオブジェクトを作成し、新しいNFCタグがスキャンされたときに、今(いくつかのサーバー側の遅延で)HTMLが正しく更新された値を表示します。
var nfc = angular.module('NfcCtrl', ['PatientRecordsService'])
nfc.controller('NfcCtrl', function($scope, NfcService) {
$scope.tagData = NfcService.tagData;
$scope.clear = function() {
NfcService.clearTag();
};
});
nfc.factory('NfcService', function($rootScope, $ionicPlatform, $filter, PatientRecordsServi\
ce) {
var tagData = {
tag: null,
patientId: null,
patientRecords: []
};
$ionicPlatform.ready(function() {
nfc.addNdefListener(function(nfcEvent) {
//console.log(JSON.stringify(nfcEvent.tag, null, 4));
$rootScope.$apply(function() {
tagData.tag = nfcEvent.tag;
tagData.patientId = $filter('decodePayload')(tagData.tag.ndefMessage[0]);
PatientRecordsService.getPatientRecords(tagData.patientId)
.then(
function(response) {
tagData.patientRecords = response
},
function(httpError) {
throw httpError.status + " : " +
httpError.data;
});
});
console.log("Tag: ", tagData.tag);
console.log("PatientId: ", tagData.patientId);
}, function() {
console.log("Listening for NDEF Tags.");
}, function(reason) {
alert("Error adding NFC Listener " + reason);
})
});
return {
tagData: tagData,
clearTag: function() {
angular.copy({}, this.tagData);
}
};
});
を私はこれを試してみるだろうが、私は、その後どのように理解していません'tag'変数は正常に更新されます(少なくともこれはHTMLページで使用するときに表示されます) –
新しい値を割り当てる 'patientId'とは異なり、' tag'を使用して変数に新しい値を渡すと、あなたは 'angular.copy()'を使ってそれを指しているオブジェクトを変更します。これは返された 'NfcService'の' tag'が指し示しているのと同じオブジェクトなので、 'NfcService'を使ったコードはその変更を見るでしょう。これは通常の古いJavaScriptプリミティブデータ対オブジェクト参照の動作です:http:// stackoverflow。com/a/13266769/60422 – rakslice
ありがとうございます。オブジェクトを作成し、HTTP GETリクエストを工場に移すことで、コントローラ内の '$ watch 'の必要性を完全に排除することができました。 –