我在使用angular with 和attributes 创建label和input元素之间的链接时遇到问题.给出以下代码:forid
@Component({
selector: 'hello',
template: `
<label [attr.for]="identifier">{{label}}</label>
<input [(ngModel)]="model" [attr.id]="identifier">
`,
})
export class HelloComponent {
@Input() identifier: string;
}
Run Code Online (Sandbox Code Playgroud)
我想要的是能够通过单击标签来聚焦输入字段,这是web中的默认行为.如果我静态设置属性,则此行为有效.或者,如果我从开发人员工具手动更新DOM中的属性.
我试过的,对于id和for:
id="{{identifier}}"
[id]="identifier"
[attr.id]="identifier"
Run Code Online (Sandbox Code Playgroud)
UPDATE
发现了问题.检查下面的答案.