每当选择ngx-datatable中的行时都会调用onSelect()
。 由于某种原因,当选择同一行时,isNodeSelected
没有得到补充,即使它输入了 if 语句。isNodeSelected
与ngIf
一起使用以显示按钮。Webstorm 突出显示了补语声明并说"缩小到真实">
onSelect({ selected }) {
this.isNodeSelected = true;
if (Array.isArray(this.selected) && this.selected.length === 1) {
if (this.selected[0].hasOwnProperty('id') && this.selected[0].id === selected[0].id) {
this.isNodeSelected = !this.isNodeSelected;
} else if (this.selected[0].hasOwnProperty('id') && this.selected[0].id !== selected[0].id) {
this.selected = selected;
this.isNodeSelected = true;
}
} else {
this.selected = selected;
this.isNodeSelected = true;
}
}
如果这个函数在你的 component.ts 文件中,那么至少你需要替换:
onSelect({ selected }) {
跟
onSelect(selected) {
因为大括号用于在模板中插入变量值,而不是在代码隐藏中插入。
除此之外,使用调试器单步执行每个位以检查每个点的值。
例如,它是否输入第二个 if 语句,如果是,selected[0].id 的值是多少等。