Angular 将不同的参数传递给角度组件的多个实例

Angular 将不同的参数传递给角度组件的多个实例,angular,Angular,因此,我创建了一个简单的角度组件自定义组件,带有一个输入参数和一个单击按钮,我只是将参数值打印到控制台 输入参数声明 @Input() componentType: string; 组件实例创建- <custom-component componentType="Type1"></custom-component> <custom-component componentType="Type2"></custom

因此,我创建了一个简单的角度组件
自定义组件
,带有一个输入参数和一个单击按钮,我只是将参数值打印到控制台

输入参数声明

@Input() componentType: string;
组件实例创建-

<custom-component componentType="Type1"></custom-component>

<custom-component componentType="Type2"></custom-component>
现在,我的问题是,无论单击哪个组件按钮,它都会记录Type1。 我尝试将debug放入ngOnInit()中,并确认在组件创建过程中传递了正确的值


但是,组件创建后,这些值在下一次操作中不会持久化(在本例中单击按钮)

您在如何使用输入将数据传递给组件方面犯了错误:

<custom-component [componentType]="Type1"></custom-component> // this way it will pass the updated data.

<custom-component [componentType]="Type2"></custom-component>


查看更多详细信息。

您能提供更多代码详细信息吗?单击事件、component.ts代码等。请共享您的单击事件的代码。更新了带有代码详细信息的问题@ApoorVachikara请将每个代码块分配给该文件。在哪里调用此处理程序?在一个视图中,Compents是如何呈现的?如果您在顶部添加一个带有Type0的3ed,是否所有单击都将返回Type0?也许第一个按钮与其他按钮重叠,而您只单击了第一个按钮?请显示CustomComponent的相关视图部分。
componentType
是字符串类型,因此我认为这是一个错误,因为它很可能是故意的(很难想象变量
Type1
/
Type2
)。不管这些是字符串还是字符串变量。首先,什么类型都是输入接收值的方式错误,@Eatos你可以检查如何将值传递给输入。其次,他的问题是他为什么接收type1,因为他只在“ngOnInit”上检查值。检查如何传递数据实际上他在init上写下了“console在这里打印Type1/Type2”和“确认传递了正确的值”。因为他没有使用单向数据绑定,而且他也没有,所以关于生命周期的部分是不相关的。这里不是说你没有给出一个好的观点,只是与给定的代码和他写的内容无关。
<custom-component [componentType]="Type1"></custom-component> // this way it will pass the updated data.

<custom-component [componentType]="Type2"></custom-component>

ngOnChanges() {
    console.log(this.componentType); // console prints the updated values over time
}