javascript - 不能在父级的 [ngClass] 逻辑中使用子级输入值
问题描述
我正在尝试将用户输入值从我的input
直通传递给父级,并使用该值来确定要在我的<li *ngFor...
元素中突出显示的行。
我实际上可以成功地传递值,所以看起来,因为我设置了一个console.log
来捕获子组件首先发出的内容,然后是父组件捕获的内容,但我只是无法通过我的[ngClass]
逻辑看到它部分...
如果我硬编码要突出显示的行的值,则逻辑确实可以正常工作,但是当然,我想以编程方式执行此操作。
父组件.ts
rowId: number;
childToParent(val){
console.log('received from child: ' + val);
this.rowId = val;
}
父组件.html
<app-child (childToParent)="childToParent($event)"></app-child>
<li *ngFor="let item of items" attr.id="item-{{item.id}}" [ngClass]="{'active': item.id === rowId}">
<div class="item">
...
</li>
child.component.ts
@Output() childToParent = new EventEmitter<String>();
sendToParent(id){
console.log('sending to parent: ' + id)
this.childToParent.emit(id);
}
child.component.html
<input [(ngModel)]="val" (input)="sendToParent(val)"/>
解决方案
你确定 item.id 和 rowId 都是数字类型吗?您可以将“===”更改为“==”,它适用于字符串和数字。
推荐阅读
- python - Openstreetmap 位置共享 python/django
- ubuntu - 安装 Flannel 后 Kubeadm join 不起作用
- python - 我们如何根据 kivy 中的屏幕大小更改小部件的大小?
- tree - 由唯一的 Prufer 序列号组成的树可以有多少个分支?
- python - 从 encodeString 生成 .xsl 文件
- r - R:R中是否有一种方法可以使用字典替换向量的值(具有旧值和新值的2列数据框)
- c - 在这种情况下,指向数组的指针如何工作?
- python - 如何根据熊猫中的最新文件过滤掉数据框?
- javascript - 不能在会话变量 express 中使用“Set”
- html - 使用最接近()方法使用jQuery设置输入类型隐藏值