简体   繁体   English

如何以角度用户观察者

[英]how to user observer in angular

Right now I am using subscribe for my Http GET request现在我正在为我的 Http GET 请求使用订阅

Question:题:

how can I use Observable which will display the data without refreshing the page?我如何使用 Observable 来显示数据而不刷新页面?

This is my Http GET method call:这是我的 Http GET 方法调用:

 this.http.get(this.tenant_userlist).subscribe(response =>{
  this.users = response;
  this.dataSource.data = this.users;
});

I am using angular material table:我正在使用角材料表:

HTML: HTML:

          <mat-table [dataSource]="dataSource" matSort>
            <ng-container matColumnDef="user_id">
              <mat-header-cell *matHeaderCellDef mat-header-cell mat-sort-header> User UID </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="User UID"> {{row.user_id}} </mat-cell>
            </ng-container>

            <ng-container matColumnDef="first_name">
              <mat-header-cell *matHeaderCellDef mat-header-cell mat-sort-header> First Name </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="First Name" class="tab-data"> {{row.first_name}} </mat-cell>
            </ng-container>

            <ng-container matColumnDef="last_name">
              <mat-header-cell *matHeaderCellDef mat-header-cell mat-sort-header> Last Name </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="Last Name"> {{row.last_name}} </mat-cell>
            </ng-container

            <ng-container matColumnDef="email_id">
              <mat-header-cell *matHeaderCellDef mat-header-cell mat-sort-header> Email ID </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="Email ID"> {{row.email_id}} </mat-cell>
            </ng-container>

            <ng-container matColumnDef="action">
              <mat-header-cell *matHeaderCellDef  > Action </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="Action"> 
                <button type="button" class="btn btn-sm shadow btnTable" >Edit</button>
              </mat-cell>  
            </ng-container>
            
            <mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
            <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
            
          </mat-table>

You can display a couple ways.您可以通过多种方式显示。

The Angular Twitter community has taken a liking to a template driven approach. Angular Twitter 社区喜欢模板驱动的方法。 Similar to @StyrianDev answer, but you won't need to subscribe to the http request in the component.类似于@StyrianDev 答案,但您不需要订阅组件中的 http 请求。

// component.ts

ngOnInit() {
    this.users = this.http.get(this.tenant_userlist);
}

And in the HTML:在 HTML 中:

// component.html

<div *ngFor='let user of users | async'>
    {{user.name}}
</div>

// mat-table
<mat-table [dataSource]="users | async" matSort>

Or you can take a component driven approach, similar to what you are doing above.或者您可以采用组件驱动的方法,类似于您在上面所做的。

ngOnInit() {
    this.http.get(this.tenant_userlist).subscribe(response =>{
        this.users = response;
        this.dataSource = this.users;
    });
}

HTML: HTML:

<div *ngFor='let user of users'>
    {{user.name}}
</div>

// mat-table
<mat-table [dataSource]="dataSource" matSort>

Note: If you want to use the AsyncPipe in your template, you'll need to import the CommonModule.注意:如果要在模板中使用AsyncPipe ,则需要导入 CommonModule。

You will want to use the async pipe as @StyrianDev mentioned, but in your case it should look like this:您将希望使用@StyrianDev 提到的异步管道,但在您的情况下,它应该如下所示:

<div *ngIf="users | async as users">
         <mat-table [dataSource]="users" matSort>
            <ng-container matColumnDef="user_id">
              <mat-header-cell *matHeaderCellDef mat-header-cell mat-sort-header> User UID </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="User UID"> {{row.user_id}} </mat-cell>
            </ng-container>

            <ng-container matColumnDef="first_name">
              <mat-header-cell *matHeaderCellDef mat-header-cell mat-sort-header> First Name </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="First Name" class="tab-data"> {{row.first_name}} </mat-cell>
            </ng-container>

            <ng-container matColumnDef="last_name">
              <mat-header-cell *matHeaderCellDef mat-header-cell mat-sort-header> Last Name </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="Last Name"> {{row.last_name}} </mat-cell>
            </ng-container

            <ng-container matColumnDef="email_id">
              <mat-header-cell *matHeaderCellDef mat-header-cell mat-sort-header> Email ID </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="Email ID"> {{row.email_id}} </mat-cell>
            </ng-container>

            <ng-container matColumnDef="action">
              <mat-header-cell *matHeaderCellDef  > Action </mat-header-cell>
              <mat-cell *matCellDef="let row" data-label="Action"> 
                <button type="button" class="btn btn-sm shadow btnTable" >Edit</button>
              </mat-cell>  
            </ng-container>

            <mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
            <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>

          </mat-table>
    </div>

This means that users should be an observable.这意味着users应该是一个被观察者。 In your ts file for this component, you should have your users property declared and you should set the value in the ngOnInit life cycle hook to return the observable from the HttpClient.Get method.在此组件的 ts 文件中,您应该声明users属性,并且应该在ngOnInit生命周期挂钩中设置值以从 HttpClient.Get 方法返回可观察对象。

users: Observable<User[]>;

ngOnInit() {
   this.users = this.http.get(this.tenant_userlist);
}

To display this.users async just use the following syntax:要显示 this.users 异步只需使用以下语法:

<div *ngFor='let user of users | async'>
    {{user.name}}
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM