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

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

相关内容

热门资讯

总算清楚!We poker辅助... 总算清楚!We poker辅助器下载(辅助挂)其实是真的有挂(的确有挂辅助插件)1、进入到是否有挂之...
必看攻略!hh poker插件... 必看攻略!hh poker插件下载(辅助挂)都是真的有挂(有挂方针辅助攻略)1、全新机制【ai辅助工...
信息共享!hhpoker俱乐部... 信息共享!hhpoker俱乐部是干嘛的(辅助挂)一贯存在有挂(有挂方略辅助脚本)1、下载好正确养号方...
一分钟了解!wepoker私人... 一分钟了解!wepoker私人局透视插件(辅助挂)都是存在有挂(新版有挂辅助app)1、上手简单,内...
盘点一款!wepoker私人局... 盘点一款!wepoker私人局透视教程(辅助挂)一直是有挂(存在有挂辅助方法)1、辅助软件下载优化,...
玩家必备科普!竞技联盟透视插件... 玩家必备科普!竞技联盟透视插件(辅助挂)好像真的有挂(有挂教学辅助攻略)1. 选牌创建新账号,点击进...
终于懂了!aapoker怎么拿... 终于懂了!aapoker怎么拿好牌(辅助挂)其实是真的有挂(确实有挂辅助技巧)终于懂了!aapoke...
玩家必看分享!wepoker有... 玩家必看分享!wepoker有脚本吗(辅助挂)本来存在有挂(有挂规律辅助工具)1)有没有挂:进一步探...
9分钟了解!we-poker软... 9分钟了解!we-poker软件(辅助挂)果然有挂(真实有挂辅助方法)1、全新机制【ai辅助工具激活...
推荐十款!wepoker有什么... 推荐十款!wepoker有什么规律(辅助挂)其实有挂(有挂工具辅助脚本)1、打开软件启动之后找到中间...