This is my HTML:
<div ng-controller="BooksController as bookCtrl">
<form ng-submit="bookCtrl.add()">
<input type="text" ng-model="bookCtrl.book.name">
<input type="text" ng-model="bookCtrl.book.rating">
<input type="text" ng-model="bookCtrl.book.type">
<input type="text" ng-model="bookCtrl.book.author">
<input type="hidden" value="fa fa-keyboard-o fa-stack-1x" ng-model="bookCtrl.book.picture">
<input type="submit" value="Add">
</form>
</div>
and this is my JS:
.controller("BooksController", ['BooksService', function(BooksService) {
var self = this;
self.getBooks = function() {
return BooksService.getBooks();
}
self.add = function(book) {
alert(book.name);
BooksService.add(book);
};
}])
.factory("BooksService", [function() {
var books = {
1: {
name: "k",
review: "k",
rating: 2,
type: "k",
author: "k",
picture: "fa fa-keyboard-o fa-stack-1x",
},
2: {
name: "b",
review: "b",
rating: 4,
type: "b",
author: "b",
picture: "fa fa-code fa-stack-1x",
}
}
return {
getBooks: function() {
return books;
},
add: function(book) {
books[4] = book;
}
};
}])
The issue here is, when I fill out the information for the form and try to add a book, the book is undefined. I tried to do
alert(book.name);
and it gave an error saying
Cannot read property 'name' of undefined.
How come it is undefined? What I want to do is take the information provided in the form and add a book to the "books" object.