As I am currently developing a prototype app using Ionic, I find myself diving into the world of Ionic and Angular. In my project, I have created a small JSON API that contains around 25 objects. I have successfully displayed these objects in a list on a page named "Library". Now, my goal is to turn each item in the list into a link that will lead to an individual page called "Lesson". However, I am encountering an issue where the variable $scope.lessonId is set properly in the controller but shows up as undefined in the service. I'm wondering if it's possible to achieve what I am aiming for or if I need to rethink my approach entirely.
.controller('LibraryLessonCtrl', function($scope, $stateParams, LessonService) {
$scope.lessonId = $stateParams.libraryId;
console.log($scope.lessonId);
LessonService.getLessonId()
.then(function(response){
$scope.lesson = response;
console.log($scope.lesson);
});
})
.service ('LessonService', function($http){
return { getLessonId: function() {
return $http.get('api/postsAPI.json')
.then(function (response, lessonId) {
console.log(lessonId);
for(i=0;i<response.data.length;i++){
if(response.data[i].post_id == lessonId){
return response.data[i];
}
}
});
}
};
})