angular - Hur man visar alla notifikationer när man använder parametrar i typescript


Jag vill visa alla mina notiser i HTML. Värdet kommer i res=response.json(); men på min sida visas bara en notis, som i

Först visa från den här koden:

  public eventsbyserial(id: string): Observable<Notifications> {
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    let urlSearchParams = new URLSearchParams();
    urlSearchParams.append('serial_device', id);
    urlSearchParams.append('token', this.auth.getCurrentUser().token);
    let body = urlSearchParams.toString();

    return this.http.post(Api.getUrl(Api.URLS.eventsbyserial), body, {
      headers: headers
    })
      .map((response: Response) => {
        let res = response.json();
        console.log(res) // this show me all notifications
        if (res.StatusCode === 0) {
          return new Notifications(res.StatusDescription[0]);
        } else if (res.StatusCode === 1) {
          this.auth.logout();
        } else {
          return new Notifications(null);
        }
      });
  }

Andra visningen för den här koden:

 notificcationserial: Notifications[]=[];
  notif: Notifications;
     getalleventsserial() {
        this.activatedRoute.params.subscribe(
          params => {
            this.ns.eventsbyserial(params['id']).subscribe(
              notificcationserial => {
                this.notif = notificcationserial;
                console.log(this.notif) // show only one notification
              }
            );
          }
        );
      }

i html:

  <table *ngFor="let item of notificcationserial ">
          <tr >
              <td> {{item.alarmnumber}}</td>
              <td> {{item.acted}}</td>       
          </tr>
   </table>

Kan du fråga mig vad problemet är, någon lösning tack?

UPPDATERING :

   if (res.StatusCode === 0) {
              return new Notifications(res.StatusDescription);

Mitt resultat ser ut som på bilden: odefinierad

Svar #1

Här returnerar du endast den första posten i din array och du lägger en array av StatusDescription i Notifications. Det fungerar inte.

[...]
    if (res.StatusCode === 0) {
      return new Notifications(res.StatusDescription[0]);
[...]

Gör det på det här sättet.

public eventsbyserial(id: string): Observable<StatusDescription> {

[...]
    if (res.StatusCode === 0) {
      return res.StatusDescription;
[...]


notif: StatusDescription;
   getalleventsserial() {
      this.activatedRoute.params.subscribe(
        params => {
          this.ns.eventsbyserial(params['id']).subscribe(
            notificcationserial => {
              this.notif = notificcationserial;
              console.log(this.notif) // supposed to show 2 rows according to your example
            }
          );
        }
     );
   }

och i din HTML gör detta

  <table *ngFor="let item of notif">
          <tr >
              <td> {{item.alarmnumber}}</td>
              <td> {{item.acted}}</td>       
          </tr>
  </table>