我開發了一個角度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一側。 –