如何在打字稿中使用params时显示所有通知

时间:2018-03-29 14:24:07

标签: angular typescript

我想在html中显示我的所有通知。 值已到达res = response.json();,但在我的网站中只显示一条通知,例如image 首先从这段代码中显示:

  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);
        }
      });
  }

此代码的第二个节目:

 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
              }
            );
          }
        );
      }

in html:

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

你能问我问题是什么,请问有什么解决方案吗?

更新:

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

我的结果就像在照片中:undefined

enter image description here

1 个答案:

答案 0 :(得分:0)

这里只返回数组中的第一个条目,并在Notifications中放置一个StatusDescription数组。这不起作用。

"allOf":{"$ref":"#definitions/link/definitions/post/definitions/single"}

这样做。

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

并在HTML中执行此操作

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
            }
          );
        }
     );
   }