简体   繁体   English

如何使用同一ts文件中的材质对话框从另一个组件清除表单数据?

[英]How can I clear form data from another component using material dialog in the same ts file?

How can I clear form data from another component using material dialog in the same ts file? 如何使用同一ts文件中的材质对话框从另一个组件清除表单数据?

Here is my .ts file 这是我的.ts文件

@Component({
  selector: 'clear-confirmation-dialog',
  templateUrl: './clear-confirmation-dialog.html'
})
export class ClearConfimationDialog {

  clearForm(){
    /** FUNCTION TO CLEAR FORM DATA **/
  }

}

@Component({
  selector: 'app-enter-user',
  templateUrl: './enter-user.component.html',
  styleUrls: ['./enter-user.component.scss']
})
export class EnterUserComponent implements OnInit {

/** THIS IS THE FORM DATA **/
user = {
  name: '',
  address: ''
}

}

Inject the EnterUserComponent to the ClearConfimationDialog EnterUserComponent注入到ClearConfimationDialog

@Component({
  selector: 'clear-confirmation-dialog',
  templateUrl: './clear-confirmation-dialog.html'
})
export class ClearConfimationDialog {

  enter_user_component:EnterUserComponent;

  clearForm(){
    /** FUNCTION TO CLEAR FORM DATA **/
   this.enter_user_component.clearForm()
  }

 constructor(@Inject(forwardRef(() => EnterUserComponent)) enter_user_component:EnterUserComponent){
        this.enter_user_component = enter_user_component
  }

}

By the way, only when the app-enter-user include the clear-confirmation-dialog will work 顺便说一句,只有当app-enter-user包括clear-confirmation-dialog时,

for example: 例如:

 enter-user.component.html:

....
<clear-confirmation-dialog></clear-confirmation-dialog>
....

You better add @Optional() & @Host() decorator to enter_user_component 您最好将@Optional()@Host()装饰器添加到enter_user_component

More about forwardRef: 有关forwardRef的更多信息:

https://angular.io/api/core/forwardRef https://angular.io/api/core/forwardRef

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular Material MatTableModule - 如何从模板中的 mat-table 外部访问 component.ts 文件中返回的数据? - Angular Material MatTableModule - How can I access the returned data from component.ts file from outside the mat-table in the template? 如何从 Angular 材料对话框中调用另一个组件 function - How can I call another component function from Angular Material Dialog 用另一个组件中的数据填充“角材料”对话框 - Populating an Angular Material Dialog with data from another component 我如何将变量从 app.component.ts 返回到另一个主页 ts ionic 3 - how i can return variable from app.component.ts to another home page ts ionic 3 如何从另一个 Component.ts 访问一个 Component.ts(在同一个 app.component.html 中) - How to access a Component.ts from another Component.ts (in the same app.component.html) 如何将输入的数据从一个html文件移动到其ts文件,然后再移动到另一个组件文件? - How to move the entered data from one html file to its ts file and then to another component file? 如何从 service.ts 文件中的 Component.ts 文件调用 function,该文件在 angular 的同一组件中 - How to Call function from Component.ts file in service.ts file which is in the same component in angular 如何将 angular 材质对话框的数据传递给父组件? - how to pass data from angular material dialog to parent component? 如何在Material 2对话框Angular 2中将数据从1个组件发送到其他组件 - How to send data in Material 2 dialog Angular 2 from 1 component to other Angular - 当它们在 component.ts 文件中启动时,如何从指令内部检测表单事件? - Angular - How do I detect form events from inside a directive when they are initiated in component.ts file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM