【问题标题】:angular: update specify part of dom by two ngFors角度:通过两个 ngFors 更新指定 dom 的一部分
【发布时间】:2018-09-24 14:22:17
【问题描述】:

我有一个问题,不知道有没有可能在 Angular 中做到这一点。

我有一个简单的 HTML 代码:

<div >
    <div *ngFor="let item of apiNames, let i = index" class="rescontainer">
        <div class="resbox headline">
            <strong>Name:</strong> {{item}}
            <span class="extra" (click)="getApiInfo($event, i)">mehr Info</span>
        </div>

        <div id="resMoreBox{{i}}">
            <div *ngFor="let item of apiRes, trackBy: trackByFn" >
                <div class="resbox" *ngIf="item['ADS Container']">
                    <strong>ADS Container:</strong> {{item['ADS Container']}}
                </div>
                ...
            </div>
        </div>
    </div>
</div>

“魔法”来自点击事件(click)="getApiInfo($event, i)。这运行一个http.get。这工作正常,结果是:

[{…}]
0: {PK: 81, IconIndex: "6_1", Name: "VKNE2004", Current Scan: null, PWD Last Set: 4, …}

结果始终是第一个 ngFor 中的一个项目的一部分:

ngFor1 = Itemname1
------ngFor2 Apires
ngFor1 = Itemname2
------ngFor2 Apires
ngFor1 = Itemname3
------ngFor2 Apires
....

现在我想更新那里的圆顶,它适合第一个 ngFor 中的项目。

!API RES get Itemname2!
ngFor1 = Itemname1
------ngFor2 Apires
ngFor1 = Itemname2
------ngFor2 Apires --> UPDATE DOM
ngFor1 = Itemname3
------ngFor2 Apires
....

问题是,角度更新第一个 ngFor 的每个 Item 中的第二个 ngFor 内容。

是的……这很正常^^

所以问题是,是否可以只更新第一个 ngFor 的特定部分?也许是名字?

我尝试使用 trackBy 但它不起作用。那么请大家有什么想法可以解决这个问题?

【问题讨论】:

  • 对我来说,apiNames 应该是一个带有属性“item”(api 名称)的“object”数组,默认情况下 extrat 为空。当您单击按钮时,您可以将 http 的结果存储到此 extrat var 中,并将第二个 ngFor 绑定在此额外 var 上;)
  • mmhh 听起来不错。但很抱歉,我没有完全理解。可以举个例子吗

标签: angular ngfor


【解决方案1】:

你可以这样做:

更改您的 apiNames 数据结构。示例:

apiNames = ['/api1', '/api2', '/api3'];

const apisData = this.apiNames.map((apiName) => {
  return {
    apiName: apiName,
    data : []
  };
});

现在在模板中使用 apisData 并将数据属性绑定到下一个 ngFor

<div *ngFor="let item of apisData , let i = index" class="rescontainer">
    <div class="resbox headline">
        <strong>Name:</strong> {{item.apiName}}
        <span class="extra" (click)="getApiInfo($event, item)">mehr Info</span>
    </div>

    <div id="resMoreBox{{i}}">
        <div *ngFor="let apiData of item.data, trackBy: trackByFn" >
            <div class="resbox" *ngIf="apiData['ADS Container']">
                <strong>ADS Container:</strong> {{apiData['ADS Container']}}
            </div>
            ...
        </div>
    </div>
</div>

然后更改 getApiInfo 实现以管理 item.data :)

getApiInfo($event, item) {
  this.httpClient.get(item.apiName + '/blablabla').subcribe( (result) => {
    item.data = result;
  });
}

【讨论】:

  • Nice :),不要忘记接受答案让人们明白这张票很接近
猜你喜欢
  • 2019-04-02
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多