简体   繁体   English

Angular4 ngIf未更新

[英]Angular4 ngIf is not updating

The template is not detecting changes to the stock model when checkbox variable is set to true. 当复选框变量设置为true时,模板未检测到库存模型的更改。 I've tried use change detection but not sure if I was implementing correctly, so I've removed from my code example for now. 我尝试使用更改检测,但是不确定我是否在正确实施,因此现在从代码示例中删除了它。 Oddly enough, I wasn't having an issue before I started pulling data from a remote service. 奇怪的是,在开始从远程服务中提取数据之前,我没有遇到任何问题。

Here is my plunker example: http://plnkr.co/edit/0EfkaSpk3Ag9p5NH5jJ2?p=preview 这是我的小矮人示例: http ://plnkr.co/edit/0EfkaSpk3Ag9p5NH5jJ2?p=preview

Here is my control: 这是我的控制权:

    <tr *ngFor="let stock of stocksObservable | async;">
      <td>
        <input [checked]="stock.isChecked" (change)="stock.isChecked = !stock.isChecked;onSelectStock($event);" type="checkbox"> <a href="">{{ stock.number }}</a>  {{stock.isChecked}} 
      </td>
    </tr>

Within my ng-container, I should see stocks that I have selected. 在我的ng-container中,我应该看到选择的库存。 Instead, I see no changes or updates... 相反,我看不到任何更改或更新...

<table>
  <tr>
    <th>Is Checked</th>
  </tr>
  <ng-container *ngFor="let stock of stocksObservable | async;">  
    <tr *ngIf="stock.isChecked">
      <td>
        <a href="">{{stock.number}}</a> {{stock.isChecked}} 
      </td>
    </tr>
  </ng-container>
</table>

Here's my class: 这是我的课:

//services
branchesObservable : Observable<object> ; 
stocksObservable : Observable<object> ; 

isChecked: boolean;
selectable: boolean;

constructor( private stocksService: StocksService ) { 
  this.branchesObservable = this.stocksService.get_branches();
}

//fetch stock data into table
getBranchStocks(value){
  this.stocksObservable = this.stocksService.get_stocks(value);

}

//select stocks
onSelectStock(event) {
  this.isChecked = !this.isChecked;
}

I think you are using the observables in a wrong way. 我认为您以错误的方式使用了可观察物。 Observables are used to emit objects. 可观察对象用于发射对象。 So, when your object inside the observable is updated when checkbox is clicked its not the same object which is used in the next ngFor and hence not reflected as its subscription model, kind of two subscriptions. 因此,当单击复选框时,如果可观察对象内部的对象被更新,则其对象与下一个ngFor中使用的对象不同,因此不会反映为其订阅模型(两种订阅)。

I created a local variable stocks which is updated or assigned to via subscribe method. 我创建了一个本地变量股票,该股票通过Subscribe方法进行更新或分配。

I have modified plnkr code: http://plnkr.co/edit/7WBNDX53pCbf44QnlRo4?p=preview . 我修改了plnkr代码: http ://plnkr.co/edit/7WBNDX53pCbf44QnlRo4?p=preview。

    // import { Component, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
    import { Component, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
    import { HttpClient } from '@angular/common/http';

    import { StocksService } from './stocks.service';
    import { Observable } from 'rxjs/Observable';



    @Component({
      selector: 'app-root',
      template: `
        <div class="wrapper">
          <select #branch (change)="getBranchStocks(branch.value)">
            <option value="null">select a branch</option>
            <option *ngFor="let branch of branchesObservable | async" value="{{ branch.branchId }}">{{ branch.name }}</option>
          </select>
          <table style="width:100%;">
            <tr>
              <th>Stock</th>
            </tr>
            <tr *ngFor="let stock of stocks">
              <td>
                <input [checked]="stock.isChecked" (change)="stock.isChecked = !stock.isChecked;onSelectStock($event);" type="checkbox"> <a href="">{{ stock.number }}</a>  {{stock.isChecked}} 
              </td>
            </tr>
          </table>
          <table style="width:49%;float:left;border:0;">
            <tr>
              <th>Is Checked</th>
            </tr>
            <ng-container *ngFor="let stock of stocks">
              <tr *ngIf="stock.isChecked">
                <td>
                  <a href="">{{stock.number}}</a> {{stock.isChecked}} 
                </td>
              </tr>
            </ng-container>
          </table>
        </div>
      `,
    })

    export class AppComponent  {

        //services
        branchesObservable : Observable<object> ; 
        stocksObservable : Observable<object> ; 
        stocks:object;

        isChecked: boolean;
        selectable: boolean;

        constructor( private stocksService: StocksService ) { 
          this.branchesObservable = this.stocksService.get_branches();
        }

        //fetch stock data into table
        getBranchStocks(value){
          this.stocksObservable = this.stocksService.get_stocks(value);
          this.stocksObservable.subscribe(data=>this.stocks=data);
        }

        //select stocks
        onSelectStock(event) {
          this.isChecked = !this.isChecked;
        }


    }

I guess you are dealing with stocks which needs local updates as well as continuous updates from subscription as well. 我猜您正在处理需要本地更新以及订阅不断更新的股票。 So, the ideal way would be to use BehaviorSubject in RxJS. 因此,理想的方法是在RxJS中使用BehaviorSubject。 A nice example of it can be found on https://coryrylan.com/blog/angular-observable-data-services 一个很好的例子可以在https://coryrylan.com/blog/angular-observable-data-services上找到

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

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