zico
zico

Reputation: 57

Angular problem of How can I show only one record in an array object?

I'm facing a problem about the when I get one record from array like this

data.service.ts

    getOneBookDetail(isbn:any) {
        const headers = new HttpHeaders().set("Content-Type", "application/json");
    
        // console.log("=============" + isbn )
        console.log(headers)
        return this.http.get('http://localhost:10888/bookdetail/?isbn='+ isbn).subscribe(
          (val) => { // Get has no error and response has body
            console.log("Get successful value returned in body", val);
          },
          response => {
            console.log("Get call in error", response);
          },
          () => { // Get has no error, response has no body
            console.log("The Get observable is now completed.");
          });
      }

home.component.ts
getBookDetail(book) {
    this.data.getOneBookDetail(book.isbn)   //isbn of book
   }

and I can click the title of book

 <a routerLink="/bookdetail/{{book.isbn}}" (click)="getBookDetail(book)"><h3>{{ book.title }}</h3></a>

and I can get a object I saw it in console

  Get successful value returned in body [{…}]
    
    0: {_id: "5fc91e5aa700213eb8c52de0", title: "A Promised Land"

[{…}] is 0: {_id: "5fc91e5aa700213eb8c52de0", title: "A Promised Land" ....

and now I want to get this object to a page call bookdetail to show only this book details but now still show all book the below is the bookdetail component

   import { Component, OnInit } from '@angular/core';
    import { DataService } from '../data.service';
    
    @Component({
      selector: 'app-bookdetail',
      templateUrl: './bookdetail.component.html',
      styleUrls: ['./bookdetail.component.scss']
    })
    export class BookDetailComponent implements OnInit {
    
      h1Style: boolean = false;
      books: Object;
    
      constructor(private data: DataService) {}
    
      ngOnInit() {
        this.data.getBooks().subscribe(data=> {
          console.log({data})  //show data
          this.books = data
          //console.log(this.books);
      })
      }
    
    }

in bookdetail html

  <h1>Book-detail</h1>
    <div *ngIf="books" class="bookdetail-block">
      <div *ngFor="let bookdetail of books" class="bookdetail">
        <h1>{{bookdetail.title}}</h1>
        <p><img [src]="bookdetail.image" ></p>
        <p>{{bookdetail.author}}</p>
        <p>{{bookdetail.price}}</p>
        <p>{{bookdetail.isbn}}</p>
        <p>{{bookdetail.description}}</p>
    
    </div>
    </div>

How can I only show I have choose?

I think the issue is in bookdetail ngOnInit()??

Upvotes: 1

Views: 598

Answers (1)

Eliseo
Eliseo

Reputation: 57939

Zico, the idea generally is that you subscribe to ActiveRouter.params IN your "detail-component", see the docs: Well you use a switchMap to, after get the parameter, make the dataService.getOneBookDetail(id). In subscribe you equal the response to a variable and ony show the variable

book:any
constructor(private activatedRoute:ActivatedRoute,private dataService:DataService){}
ngOnInit() {
  this.route.paramMap.pipe(
    switchMap(params => {
      const ibs=params.get('isbn');
      return this.dataService.getOneBookDetail(ibs);
    }).subscribe(res=>{
      book=res;
    })
  );
}

Other idea is pass data between routes like show Netanel Basal

Upvotes: 1

Related Questions