我正在使用angularjs 4.3.4通过角度CLI工作正常。但是我越来越奇怪的问题。我用复选框和标签创建了一个自定义组件。 id和属性值来自自定义标签属性。当我看到html代码时,我可以看到它已经按照预期生成了id和for属性。但点击标签不会检查或取消选中该复选框。这是我的代码:Angualr 4x标签不起作用
input.component.html(选项1)
<div class="form-group input-group">
<span>
<input id="{{id}}" type="checkbox" placeholder="Name"/>
<label for="{{id}}">{{title}}</label>
</span>
</div>
input.component.html(选项2)
<div class="form-group input-group">
<span>
<input bind.id="id" type="checkbox" placeholder="Name"/>
<label bind.for="id">{{title}}</label>
</span>
</div>
input.component.html(选项3)
<div class="form-group input-group">
<span>
<input [id]="id" type="checkbox" placeholder="Name"/>
<label [for]="id">{{title}}</label>
</span>
</div>
input.component.html(方案4)的上述选项
<div class="form-group input-group">
<span>
<input attr.id="id" type="checkbox" placeholder="Name"/>
<label attr.for="id">{{title}}</label>
</span>
</div>
无正在工作。
input.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'my-input',
templateUrl: './input.component.html',
styleUrls: ['./input.component.less']
})
export class InputComponent implements OnInit {
@Input() title: string;
@Input() id: string;
constructor() { }
ngOnInit() {
}
}
app.component.html
<my-input title="Testing" id="checkBox"></my-input>
请帮帮我,我该怎么解决呢?
感谢
我不能使它工作。在这里,我尝试了一次强盗,但它根本不工作。我不知道我做错了什么。 https://plnkr.co/edit/QZjkT47TnKIVRaZRvSNi?p=preview – Riy
我解决了你的问题,看看https://plnkr.co/edit/ycvUFl39KCafepYgLjOz?p=preview – Vega
谢谢维加解决我的问题:)。然而,这不是问题。我刚刚添加了我的答案 – Riy