Angular模板驱动表单 - 如何将父组件绑定到子组件
创始人
2024-10-28 13:31:51
0

要将父组件绑定到子组件,可以使用Angular模板驱动表单的方式来实现。下面是一个示例代码:

父组件的模板:

父组件的代码:

import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent {
  parentValue: string;
}

子组件的模板:

子组件的代码:

import { Component, Input } from '@angular/core';
import { NgForm } from '@angular/forms';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  @Input() parentForm: NgForm;
  childValue: string;
}

在这个示例中,父组件中的输入框使用[(ngModel)]来绑定parentValue属性。然后,将父组件的表单引用#parentForm传递给子组件,子组件中的输入框使用[(ngModel)]绑定childValue属性,并且使用[ngModelOptions]="{ standalone: true }"来将子组件的输入框设置为独立的表单控件。

通过这样的设置,父组件的表单控件和子组件的表单控件可以共享数据。

相关内容

热门资讯

第四分钟辅助!werplan脚... 第四分钟辅助!werplan脚本,wepoker透视脚本免费下载pc,技法教程(揭秘有挂)在进入we...
六分钟辅助!佛手大菠萝辅助,w... 六分钟辅助!佛手大菠萝辅助,wepoker轻量版透视,教材教程(有挂头条)1)wepoker轻量版透...
第二分钟辅助!pokemmo内... 第二分钟辅助!pokemmo内置修改器,wejoker辅助软件,妙招教程(有挂详情)1、首先打开po...
第7分钟辅助!wepoker轻... 第7分钟辅助!wepoker轻量版透视系统,aapoker ai插件,绝活教程(有挂规律)1、该软件...
十分钟辅助!菠萝德州透视脚本,... 十分钟辅助!菠萝德州透视脚本,wpk可以作弊吗,项目教程(证实有挂)1、下载好菠萝德州透视脚本正确养...
六分钟辅助!wepoker有插... 六分钟辅助!wepoker有插件吗,wepoker科技辅助器,模板教程(有挂神器)1、实时wepok...
第九分钟辅助!wpk透视工作室... 第九分钟辅助!wpk透视工作室,竞技联盟透视,学习教程(真的有挂)1、wpk透视工作室有没有辅助教程...
第三分钟辅助!wepoker破... 第三分钟辅助!wepoker破解工具,购买的wpk辅助在哪里下载,妙计教程(有挂讲解)购买的wpk辅...
五分钟辅助!约局吧德州透视,拱... 五分钟辅助!约局吧德州透视,拱趴大菠萝开挂方法,策略教程(有挂方针)1、上手简单,内置详细流程视频教...
第七分钟辅助!wepoker透... 第七分钟辅助!wepoker透视挂底牌,hhpoker软件靠谱吗,妙计教程(详细教程)1、这是跨平台...