【发布时间】:2021-01-27 16:18:14
【问题描述】:
当用户单击父组件中存在的提交按钮时,我需要将输入的值从子组件传递到父组件。
childComp 模板
<input
type="password"
[(ngModel)]="userPasswordForm.inputId"
class="mr-password-field k-textbox"
/>
childComp TS 文件
export class PasswordInputComponent{
constructor() { }
@Output() inputValue = new EventEmitter<string>();
userPasswordForm:any={'input':''};
emitValue(value: string) {
this.inputValue.emit(value);
}
}
父组件模板
<child-component (inputValue)="" > </child-component>
<button (click)="getValueFromChild()"> </button>
父组件 TS 文件
tempUserFormPasswords:any=[];
.
.
.
getValueFromChild(receivedVal){
this.tempUserFormPasswords.push(receivedVal);
}
如果按钮存在于子组件中,则很容易 dio。但是在这种情况下,应该在单击父组件中的按钮时传递值!
【问题讨论】:
-
我不明白。当您从父组件单击
getValueFromChild时,您想将一个值推送到tempUserFormPasswords数组中,但要推送的值来自子组件?
标签: javascript angular parameter-passing angular-event-emitter