Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Angular 6 query

  • 20-11-2018 05:56PM
    #1
    Registered Users, Registered Users 2 Posts: 518 ✭✭✭


    Hi, just started into Angular 6 coding and have a question which I'm sure is pretty basic.

    I have an api call that populates an array which is used to with an ngFor to output a list of divs...
    [B]Controller snippet:[/B]
    
    books: Book[];
    bookContent: BookContent[];
    
    ngOnInit() {
        this.getBooks();
      }
    
      getBooks(): void {
        this.webApiService.GetBooks()
          .subscribe(x => this.books = x);
      }
    
      getBookContent(book: book): void {
        this.webApiService.GetBookContent(book.Id)
          .subscribe(x => this.bookContent = x);
      }
    
    
    [B]View snippet:[/B]
    
    <div *ngFor="let book of books">
      <div>
        <span>Book name: {{book.Name}}</span>
        <span><button (click)=getBookContent(book)>View details</button>/span>
      </div>
    </div>
    
    <div *ngIf="bookContent?.length > 0">
          <div *ngFor="let content of bookContent">{{content.Details}}</div>
    </div>
    

    (ignore fact that bookContent is an array for now)

    This works fine, and updates the one "bookContent" div whenever I click each of the buttons.

    What I want to do, though, is have a bookContent div for each book, rather than all of them using one.


    e.g.

    [B]View snippet:[/B]
    
    <div *ngFor="let book of books">
      <div>
        <span>Book name: {{book.Name}}</span>
        <span><button (click)=getBookContent(book)>View details</button>/span>
        <div>[I][B]RELATED BOOK CONTENT GOES HERE WHEN BUTTON CLICKED[/B][/I]</div>
      </div>
    </div>
    

    i.e. the content will appear in the same div as the book name, and it should be possible to see several book content inner divs at once.

    My Googling is proving fruitless, but I'm assuming this is pretty straightforward to achieve?


Comments

  • Registered Users, Registered Users 2 Posts: 8,488 ✭✭✭Goodshape


    Plenty of ways to skin a cat, but how about something like this:
    Controller snippet:
    
    books: Book[];
    bookContent: { id: number, content: BookContent }[] = [];
    
    ngOnInit() {
        this.getBooks();
      }
    
      getBooks(): void {
        this.webApiService.GetBooks()
          .subscribe(x => this.books = x);
      }
    
      getBookContent(book: book): void {
        this.webApiService.GetBookContent(book.Id)
          .subscribe(x => this.bookContent = x);
      }
    
      hasBookContent(book): boolean {
        return !!this.bookContent.find(x => x.id === book.Id);
      }
    
      bookContent(book): BookContent {
        return this.bookContent.find(x => x.id === book.Id).content;
      }
    
    
    View snippet:
    
    <div *ngFor="let book of books">
      <div>
        <span>Book name: {{book.Name}}</span>
        <span><button (click)=getBookContent(book)>View details</button>/span>
    
        <div *ngIf="hasBookContent(book)">
            <div *ngFor="let content of bookContent(book)">{{content.Details}}</div>
        </div>
      </div>
    </div>
    
    


  • Registered Users, Registered Users 2 Posts: 518 ✭✭✭St1mpMeister


    I'll give it a go thanks!


Advertisement