Hello Guys,
angular.module('starter.services', [])
.run(function($rootScope) {
$rootScope.fueherscheinBoolean = false;
$rootScope.kunden = [];
$rootScope.id = 0;
});
angular.module('starter.controllers', [])
.service("KundenService", function($http, $rootScope) {
this.loadKundenJson = function() {
$http.get('data/personen.json').success(function(data) {
data.forEach(function(obj) {
$rootScope.kunden.push(obj);
++$rootScope.id;
});
}).error(function() { console.log("LoadKundenJson Error") });
}
this.localStorage = function() {
var kundenLocalStorage = JSON.parse(localStorage.getItem('kundenLocalStorage'));
var id = (localStorage.getItem('id'));
if (kundenLocalStorage && id) {
$rootScope.id = id;
$rootScope.kunden = kundenLocalStorage;
return true;
}
return false;
}
});
.controller('kundenCtrl', function($scope, $http, $rootScope,KundenService) {
function initList(){
if(!KundenService.localStorage()){
KundenService.loadKundenJson();
}
$scope.kunden = $rootScope.kunden;
console.log($rootScope.kunden);
console.log($rootScope.kunden.length);
console.log($rootScope.id);
}
$rootScope.$watch('kunden',function(){
initList();
});
})
console.log($rootScope.kunden):
returns: the right array
console.log($rootScope.kunden.length):
returns: 0
console.log($rootScope.id):
returns: 0
If i print the id write after the foreach-loop in the kundenservice service it returns the right value (10)
Could you tell me what im doing wrong? (Ionic v1)
Ty guys!