0

I'm on Angular 2 and coded a class like so:

export class Book{
     title: string;
     author: string;
     read: integer;
     private: _author;

    constructor(author: string) {
        this._author = author
    }

     public incRead(times: integer){
        this.read = this.read + times;
     }
}

Then to get data from the server at a component:

this._httpClient.Get<Book[]>(url).subscribe(
     (books: Book[]) => {
         this.books = books;
     }
)

But I can't access the function book.incRead(3) in any of the members of the Books array in the component.

Maybe HttpClient Get method does not instantiate correctly or I'll have to map to another array to get access to the public method in the class.

I've tried some workaround but code gets unnecessarily dirt.

Please be kind. I have really done my homework and been unable to find a question about as clear as this.

EDIT

I've changed the class a bit and now I can go this way:

export class Book{
     title: string;
     author: string;
     read: integer;
     author: string;

    constructor() {
    }

     public incRead(times: integer){
        this.read = this.read + times;
     }

}

And to get data from the server (using this approach: HttpClient not running constructor) I can use Object assign

this._httpClient.Get<Book[]>(url).subscribe(
    (books: Book[]) => {
         const tempbooks = new Array<Book>();
         books.forEach(book => {
             tempbooks.push(Object.assign(new Book(),book);
        }
     }
)

Now it would be perfect adding a post function in the class using the Http service, but I'll open it in a new question.

Happy coding!

Josep.

2
  • what is the expected behavior? Commented Dec 18, 2017 at 7:07
  • I'm trying to use the incRead method on each object generated by the HttpClient get function. When httpClient gets the data (book array, only properties) the method is not available. Commented Dec 18, 2017 at 7:11

1 Answer 1

1

You have to actually make book objects:

this._httpClient.Get<Book[]>(url).subscribe(
     (books: Book[]) => {
         this.books = books.map((b) => new Book(b));
     }
)

You have to create a constructor that takes an 'any' object:

constructor(book?: any) {
  if (book != null) {
    this.title = book.title;
    this.author = book.author;
    this.read = book.read;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have edited the code to add the constructor -and get it closer to the real behavior- where a property is initialized. Now the new Book(b) shows an error about the constructor parameters.
I'm sorry I can't mark as answered immediately. I need to test it, and the real class has a lot of properties and methods. Just let me try!
Well, just going down the road. I've found this stackoverflow.com/questions/46570775/… that avoids to create the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.