【发布时间】:2018-02-14 10:39:06
【问题描述】:
我开发了一个 Angular cli 项目并获得了很多表单来验证。我需要在显示时将表单验证消息附加到 app-root 之外。 我目前的实现如下。
<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? 中插入一些脚本标签
-
您基本上需要将数据从一个组件传递到另一个组件。你可以在这里阅读更多关于它的信息angular.io/guide/component-interaction
-
请检查更新的问题。它不是关于在组件之间共享数据。它是关于将组件模板附加到 app-root 元素的 pout 侧。
-
您想将验证消息发送到其他组件吗?
-
这不是将验证消息传递给另一个组件。请在ng-bootstrap.github.io/#/components/popover/examples 的正文部分检查 Append popover。我需要将我的消息添加到正文。
标签: javascript jquery twitter-bootstrap angular angular-cli