如何在将对象属性作为参数传递时使用连字符



我正在尝试将对象属性作为参数传递。对象键包含连字符。

对象

{
"section-id": 1,
...
}

.HTML

<div *ngFor="let section of sections"
(trackScrollLeave)="leave(section.section-id)"></div>

组件

ngOnInit() {
this.retrieveDataService.fetchData().subscribe(data=>{
this.sections = data;
});
}
leave(value) {
console.log('Scroll left '+ value);
}
}

它在控制台中输出NAN。如果使用骆驼壳sectionId,它可以工作。

代码将其读取为 section.section - id (未定义 - 未定义 = NaN( 尝试

<div *ngFor="let section of sections (trackScrollLeave)="leave(section['section-id'])"></div>

括号属性访问器上的资源 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

最新更新