I'm currently facing an issue where my controller is unable to load an object from a service called notesFactory. The console.log(typeof notesFactory) statement returns 'undefined', causing notesObjectInService to trigger an exception.
Despite reviewing the code multiple times, I can't pinpoint the error. As a newcomer to javascript and AngularJS, I might be overlooking something crucial. My suspicion initially fell on the postPromise, but I still struggle to grasp the intricacies of promises.
If anyone has insights or solutions to this problem, your help would be greatly appreciated.
function NotesProvider(){
this.$get= angular.noop
}
angular.module('theNotesApp2',['ui.router'])
.config(['$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider){
$stateProvider
.state('home', {
url: '',
//the templateUrl value is the id for the routing
templateUrl: '/home.html',
controller: 'mainCtrl',
resolve: {
postPromise: ['notesFactory', function(notesFactory){
return notesFactory.getAll();
}]
}
})
.state('navbar',{
url: '/navbar',
templateUrl: '/navbar.html',
controller: 'mainCtrl'
})
.state('notes',{
url: '/notes/{id}',
templateUrl: '/notes.html',
controller: 'notesCtrl'
});
$urlRouterProvider.otherwise('home');
}
])
.factory('notesFactory', NotesProvider,'$http' [function($http, NotesProvider){
var notas = {notesObjectInService: []};
notas.getAll() = function() {
return $http.get('/notes.json').success(function(data){
angular.copy(data, notas.notesObjectInService);
})
};
notas.create() = function(note){
return $http.post('/notes.json', note).success(function(data){
notas.notesObjectInService.push(data);
})
};
return notas;
}])
.controller('mainCtrl',['$scope', 'notesFactory', function($scope, notesFactory){
console.log(typeof notesFactory);
$scope.notes = notesFactory.notesObjectInService;
$scope.addNote = function(){
if ($scope.title === "" ) {
return;
}
notesFactory.create({
title: $scope.title,
body:$scope.body
});
$scope.title= '';
$scope.body= '';
};
}])
.controller('notesCtrl', ['$scope', 'notesFactory', '$stateParams', function(
$scope, notesFactory, $stateParams){
console.log('test');
$scope.note =
notesFactory.notesObjectInService[$stateParams.id]
}])