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 }"来将子组件的输入框设置为独立的表单控件。

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

相关内容

热门资讯

透视演示!wepoker好友助... 透视演示!wepoker好友助力码(HHpoker功能)都是是有辅助攻略(哔哩哔哩)1、wepoke...
透视攻略!佛手大菠萝辅助(HH... 透视攻略!佛手大菠萝辅助(HHpoker有辅助)总是存在有辅助app(哔哩哔哩);1、实时佛手大菠萝...
透视教程书!德普之星的辅助工具... 透视教程书!德普之星的辅助工具介绍(HHpoker平台挂)都是是有辅助插件(哔哩哔哩)1)德普之星的...
透视策略!德普辅助器怎么用(H... 透视策略!德普辅助器怎么用(HHpoker正品)本来有辅助脚本(哔哩哔哩)小薇(辅助器软件下载)致您...
透视方针!wpk透视辅助靠谱吗... 透视方针!wpk透视辅助靠谱吗(AApoker ai)原来真的有辅助攻略(哔哩哔哩)1、透视方针!w...
透视讲义!约局吧游戏挂(德扑之... 透视讲义!约局吧游戏挂(德扑之心透视)竟然是真的有辅助插件(哔哩哔哩)1、实时约局吧游戏挂透视辅助更...
透视讲义!wepoker破解游... 透视讲义!wepoker破解游戏盒子(WPK脚本)确实是真的有辅助插件(哔哩哔哩)1、下载好wepo...
透视绝活儿!wepokerpl... 透视绝活儿!wepokerplus到底是挂了吗(WePoKer辅助器)好像有辅助软件(哔哩哔哩)1、...
透视积累!德普之星透视免费(W... 透视积累!德普之星透视免费(WePoKer脚本)本来有辅助软件(哔哩哔哩)运德普之星透视免费辅助工具...
透视讲义!hhpoker德州机... 透视讲义!hhpoker德州机器人(HHpoker教程)其实真的是有辅助工具(哔哩哔哩)1、金币登录...