问题描述
我有一组称为报告的数据,其中包含一组网络(report.networks).在返回之前,我有model.ts来操纵网络数组.我做了一个ngFor来遍历网络数据以显示可以正常工作的详细信息.但是,不会显示在ngFor中添加matToolTips的情况.
I have a set of data called reports which contain an array of networks (report.networks). I have model.ts to manipulate the networks array before i return it back. I do an ngFor to iterate over the network data to display the details which works fine. However, adding a matToolTips within the ngFor does not get displayed.
component.html
<span matTooltip="Tooltip Works">Tooltip Works</span>
<div *ngFor="let network of report.networks">
<span matTooltip="Tooltip Does NOT work!">Tooltip Does NOT work</span>
</div>
component.ts
import { Report } from './../../core/models/report/report.model';
@Component({
selector: 'app-report-detail',
templateUrl: './report-detail.component.html',
styleUrls: ['./report-detail.component.scss']
})
export class ReportDetailComponent implements OnInit {
report: Report;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.report = this.route.snapshot.data.report;
console.log(this.report);
}
}
report.model.ts
export class Report {
networks?: any = null;
constructor(data?: Report) {
if (data) {
this.deserialize(data);
}
}
private deserialize(data: Report) {
const keys = Object.keys(this);
for (const key of keys) {
if (data.hasOwnProperty(key)) {
this[key] = data[key];
}
}
}
get networks() {
let n = this.networks;
//some manipulation
return n
}
}
推荐答案
如此处所述 https://github.com/angular/material2/issues/10039
在ngFor中使用函数时,会一次又一次创建数组实例,这会导致多个问题,例如matToolTip不会同时显示性能问题.
The array is instance being created over and over again when using a function in a ngFor which causes multiple issues like matToolTip not displaying as-well as performance issues.
解决这个问题的另一种方法是更改model.ts文件.因此,在下面的即时消息中,将变量分配给函数的结果.然后,我直接在函数的ngFor istead中使用此变量.
Another way i got around this is to change my model.ts file. So below im assigning a variable to the result of the function. Then i use this variable inside the ngFor isntead of the function directly.
export class Report {
networks?: any = null;
//custom
customNetworks? : any = null;
constructor(data?: Report) {
if (data) {
this.deserialize(data);
this.customNetworks = this.generateNetworks()
}
}
private deserialize(data: Report) {
const keys = Object.keys(this);
for (const key of keys) {
if (data.hasOwnProperty(key)) {
this[key] = data[key];
}
}
}
generateNetworks() {
let n = this.networks;
//some manipulation
return n
}
}
这篇关于使用ngFor函数时,不显示Angular Material matToolTip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!