Hi guys, I've got an issue with my angular application, I've got a form which is meant to create a new record (object) and push this object into an array.
My set up is the following:
I have a book.component.ts whic contains an array of IBooks as such:
books: IBook[] = [
{
"bookID":356,
"bookAuthor":"John Beck",
"bookTitle":"The story of my life",
"location":"Shelf downstairs",
"medium":"paper",
"notes":""
},
{
"bookID":900,
"bookAuthor":"Jack Mills",
"bookTitle":"They",
"location":"Shelf downstairs",
"medium":"PDF",
"notes":"This is on my laptop and phone"
}
];
This file also contains the static method
static createNewRecord($record): void{
console.log("createNewRecord() called");
console.log($record);
this.books.push($record);
console.log($record);
// $event.preventDefault();
}
This method is static because it gets called from another component - the book-form.component - which is essentially a form which allow users to create a new books' object. The idea is that when the users submits the form the createNewRecord is called and adds the record to the books array. Now, when I run the application the compiler tells me that
"books.component.ts (51,14): Property 'books' does not exist on type 'typeof BooksComponent'." which kind of makes sense because I'm trying to access a non-static member (books) from a static method createNewRecord. My question is, how would I get around that?
Like I said the reason why I made that method static is because I needed to access it from another component.
I suppose I could make the array static too, but I'm not sure that's the right way to go. What do you guys think?