【问题标题】:ngFor length in Angular 5ngFor Angular 5 中的长度
【发布时间】:2018-08-28 12:52:57
【问题描述】:

我有三个按钮:

所有(Reals+ Fakes),Reals(总数),Fake(总数)

我正在尝试获取将显示在“全部”中的 Feed 总数。

以及 feed.feed_type != '' 的总计数将显示为 Reals。

以及 feed.feed_type == '' 的总计数将显示为 Fakes。

Feed 模型

export class Feeds {
  feed_id: string;
  feed_category: string;
  feed_title: any;
  feed_description: string;
  feed_terms: string;
  feed_type: string;
  checked: false;
  }

Feed 组件:

import { Component, OnInit } from '@angular/core';
import { environment } from '../../environments/environment';
import { MyService } from '../shared/services/my-service.service';
import { FeedsService } from '../shared/services/feeds.service';
import { Feeds } from '../shared/services/feeds';
import { Feed } from '../shared/services/feed';
import { Observable } from 'rxjs/Observable';


@Component({
  selector: 'app-feeds',
  templateUrl: './feeds.component.html',
  styleUrls: ['./feeds.component.scss']
})

export class FeedsComponent implements OnInit {

  feeds: Observable<Feed[]>;
  Reals: boolean;
  Fakes: boolean;
  selectedFeedType = '';


  constructor(private myService: MyService, private feedsService: FeedsService) {
    this.selectedFeedType = 'All';
    this.Reals = true;
    this.Fakes = true;
  }

  ngOnInit() {
    this.feeds = this.myService.feeds;
    this.myService.loadAll();
  }


  SelectedFeedsType(event: any) {
    this.selectedFeedType = event.target.value;
    if (this.selectedFeedType === 'All') {
      this.Reals = true;
      this.Fakes = true;
    } else if (this.selectedFeedType === 'Reals') {
      this.Reals = true;
      this.Fakes = false;
    } else if (this.selectedFeedType === 'Fakes') {
      this.Reals = false;
      this.Fakes = true;
    }
  }

}

我的服务:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { environment } from '../../../environments/environment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { FeedsService } from './feeds.service';
import { Feed } from './Feed';

@Injectable()
export class MyService {
  feeds: Observable<Feed[]>;
  private _feeds: BehaviorSubject<Feed[]>;
  private baseUrl: string;
  total = '';
  private dataStore: {
    feeds: any
  };

    constructor(private http: HttpClient) {
      this.baseUrl  = environment.API_ENDPOINT + 'feeds';
      this.dataStore = { feeds: [] };
      this._feeds = <BehaviorSubject<Feed[]>>new BehaviorSubject([]);
      this.feeds = this._feeds.asObservable();
    }

   

     loadAll() { 
        this.http.get(this.baseUrl).subscribe(feeds => { 
        this.dataStore.feeds = feeds;
        console.log(feeds.length);
        const Reals = feeds.filter(feed => feed.feed_type !== '').length; 
        console.log(Reals);
        const Fakes = feeds.length - Reals;
        console.log(Fakes);
        this._feeds.next(Object.assign({}, this.dataStore).feeds);},
        error => console.log('Could not load feeds.'));
      }

    change(feeds) {
      this._feeds.next(feeds);
    }

}

Feeds.Component.html

<ul>
  <li><input  type="radio"  name="feedType" value="All" checked (change)="SelectedFeedsType($event)">All</li>
  <li><input  type="radio"  name="feedType" value="Reals"  (change)="SelectedFeedsType($event)">Reals</li>
  <li><input  type="radio"  name="feedType" value="Fakes"  (change)="SelectedFeedsType($event)">Fakes</li>
</ul>

<table class="table table-bordered">
    <thead>
      <tr><th>Feeds</th></tr>
    </thead>
    <tbody>
      <tr><td>
        <div class="storetabContent"  *ngFor="let feed of feeds | async">
        <ng-container *ngIf="Reals && feed.feed_type != ''">
          <p>{{ feed.feed_title }}</p>
          <p>{{ feed.feed_category }}</p>
          <b>REAL</b>
        </ng-container>
        <ng-container *ngIf="Fakes && feed.feed_type == ''">
            <p>{{ feed.feed_title }}</p>
            <p>{{ feed.feed_category }}</p>
            <b>FAKE</b>
        </ng-container>
        </div>
      </td></tr>
    </tbody>

</table>

任何建议,感谢帮助。

【问题讨论】:

  • 没有你的组件代码就无法回答这个问题。请参阅 minimal reproducible exampleHow to Ask 一个好问题。
  • 也就是说:如果我不得不猜测解决方案的方向,它可能涉及订阅组件中的 observable 并计算那里的总数并将它们保存为局部变量。但是如果没有您的组件代码和您使用的数据结构,任何人都可以猜测。
  • 我真的不建议拥有两个 ng 容器。它们显示相同的确切信息、标题、类别和名称。遵循 DRY 原则。相反,请考虑在其他地方过滤您的数据。然后,记数对你来说会容易得多。否则,您可以在最初获取数据并以这种方式计数时尝试这样做。
  • @Igor 我已经用组件和服务代码更新了我的问题。
  • 能否也包括Feed 的结构。接口结构和/或 json 就足够了。

标签: angular count angular5 ngfor


【解决方案1】:

根据您的 cmets,要检索 Feed_AFeed_B 的计数,您可以使用 Array#filterArray#length

const feedACount = feeds.filter(feed => feed.feed_type !== '').length;
const feedBCount = feeds.length - feedACount;

【讨论】:

  • @Muirik 感谢您的编辑建议。我接受了它,但是在 JS 和 TypeScript 中,语句末尾的分号是可选的(虽然不知道从什么时候开始,我想至少有一段时间)。
  • 确实它们是可选的。然而,仍然被认为是包含它们的最佳做法。
  • @Muirik 取决于你问的是谁。总的来说,人们似乎对此意见不一。无论如何,这不是这个问题的重点(在这里根本不相关),所以让我们继续吧:)
  • @Muirik,谢谢我在我的服务中尝试了你的代码:loadAll() { this.http.get(this.baseUrl).subscribe(feeds =&gt; { this.dataStore.feeds = feeds; console.log(feeds.length); const Reals = feeds.filter(feed =&gt; feed.feed_type !== '').length; console.log(Reals); const Fakes = feeds.length - Reals; console.log(Fakes); this._feeds.next(Object.assign({}, this.dataStore).feeds); }, error =&gt; console.log('Could not load feeds.')); }我想将此值传递给我的组件,请检查我更新的代码,
  • 澄清一下,这是@Jeto 的回答,不是我的。
猜你喜欢
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 2019-02-08
  • 2019-04-22
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
相关资源
最近更新 更多