Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为角度模板html提供动态ID_Javascript_Angular_Angular8 - Fatal编程技术网

Javascript 为角度模板html提供动态ID

Javascript 为角度模板html提供动态ID,javascript,angular,angular8,Javascript,Angular,Angular8,我创建了一个html模板,在父组件中重复使用了几次,但传递了不同的数据 我有parent.component.html,它有 <child-component title="one"></child-component> <child-component title="two"></child-component> <child-component title="three"></child-compone

我创建了一个html模板,在父组件中重复使用了几次,但传递了不同的数据

我有parent.component.html,它有

    <child-component title="one"></child-component>
    <child-component title="two"></child-component>
    <child-component title="three"></child-component>

这很好用

问题是,在“子组件”中,我有复选框和相应的标签。正如您所知,他们需要属性知道何时选中复选框。正如我所意识到的,每次调用时,Angular都会重新渲染子组件,就像克隆一样,并且每个子组件的for属性都是相同的,所以复选框不起作用。我希望能够在每次调用子组件时为每个复选框创建动态和id


我尽力解释我的问题。这样的解决方案可行吗?

受@mynameisx comment的启发,这个解决方案比我想象的要简单

由于我使用不同的值传递了title属性,因此它本身区分了每个子组件。因此,在每个子组件中,我为输入的id和类型编写了以下代码:

<input type="checkbox" id="{{ title }} + Check">
<label for="{{ title }} + Check"></label>


复选框起作用了

您可以共享您的子组件代码吗?可以。这是可能的。您需要根据输入装饰器动态设置复选框。假设您的孩子有一个@Input checkBoxId:String='1'。在复选框上我知道,但它没有,也不会。