我开发了一个角度cli项目并获得了许多表单来验证。显示时,我需要在应用根的外侧附加表单验证消息。 我目前的实施情况如下。Angular 2将HTML附加到主体
<app-root class="" _nghost-c0="" ng-version="4.3.4">
<!-- other html content goes here...... -->
<form [formGroup]="form">
<label for="name">Name: </label>
<input type="text" [formControl]="nameCtrl"/>
<div *ngIf="!nameCtrl.valid && nameCtrl.hasError('required')"
class="error"><validation-msg>Name is required.</validation-msg></div>
<div *ngIf="!nameCtrl.valid && nameCtrl.hasError('badName')"
class="error"><validation-msg>Name must start with <tt>pee</tt>.</validation-msg></div>
</form>
<!-- other html content goes here...... -->
</app-root>
我只需要在html中显示如下所示的错误信息。 validation-msg是具有简单模板的组件。该内容必须附加到应用程序根目录之外。
<app-root class="" _nghost-c0="" ng-version="4.3.4">
<!-- other html content goes here...... -->
</app-root>
<validation-msg-content>
<div>Name is required.</div>
</validation-msg-content>
我不对Angular知之甚少,但你会需要一种“临时解决方法”来实现这一点。 也许在_if_中插入一些脚本标签? –
您基本上需要将数据从一个组件传递到另一个组件。你可以在这里阅读更多关于它的信息https://angular.io/guide/component-interaction – KIA
请检查更新后的问题。它不是关于组件之间共享数据。它是关于将组件模板附加到app-root元素的pout一侧。 –