Ich baue eine App mit Angular JS, und ich möchte einige spezifische Daten in der LocalStorage erhalten, um sie später abrufen zu können. Die Sache ist, dass ich Probleme mit dem Code habe, um das zu erreichen. Ich versuche, beim Klicken auf eine Schaltfläche mehr Assistenten zu einem Ereignis hinzuzufügen, aber der Inspektor gibt immer diesen Fehler aus: SyntaxError: Unerwartetes Token u in JSON an Position 0 bei Object.parse (nativ).LocalStorageModule in Angular JS zum Speichern von Daten
Ich habe eine controller.js getrennt von der app.js und dann habe ich die Controller in meinem app.module injiziert.
Es wäre toll, wenn Sie mir helfen können. Danke im Voraus. Hier
ist das Stück Code, das mir Fehler gibt:
'use strict';
/* Controllers */
angular.module('Erasmore.controllers', [])
.controller('Posts', ['$scope', '$http',
function($scope, $http, localStorageService) {
$http.get("json/posts.json").success(function(data) {
$scope.posts = data;
});
$scope.assistants = [{
id: 1,
name: "James Smith",
nationality: "English"
}, {
id: 3,
name: "Juan García",
nationality: "Spanish"
}, {
id: 3,
name: "Eduardo Cruz",
nationality: "Spanish"
}, {
id: 4,
name: "Jurgen Low",
nationality: "German"
}, ];
var newParticipant = localStorage['assistantList'];
if (newParticipant !== undefined) {
$scope.newAssistant = JSON.parse(newParticipant);
}
$scope.newAssistant = {};
$scope.addAssistant = function() {
$scope.assistants.push($scope.newAssistant);
$scope.newAssistant = "";
localStorage['assistantList'] = JSON.stringify($scope.newAssistant);
console.log(localStorage);
};
}
])
<div>
<p>Are you coming?</p>
<input placeholder="Enter your name" id="name" type="text" class="validate" ng-model="newAssistant.name">
<input placeholder="Enter your nationality" id="nationality" type="text" class="validate" ng-model="newAssistant.nationality">
<button class="waves-effect waves-light btn" ng-click="addAssistant()">Confirm</button>
<button class="waves-effect waves-teal btn-flat modal-trigger">See Assistants</button>
<ul>
<li id="asistentes" ng-repeat="assistant in assistants">{{assistant.name}} <span>-</span> {{assistant.nationality}}</li>
</ul>
'' localStorageService'' fügen Sie einfach Ihren Controller so hinzu; '.controller ('Beiträge', ['$ scope', '$ http', 'localStorageService', Funktion ($ Scope, $ http, localStorageService) {' –
Ja, ich erkannte später, aber es funktioniert immer noch nicht ... –