简体   繁体   English

在Ionic 3 / Angular4中有条件地将表单添加到html模板中

[英]Conditionally adding a form to a html template in Ionic 3/Angular4

I had a screen which I wanted to use in different modes.. Add/Change/Inquiry/Delete. 我有一个想要在不同模式下使用的屏幕。添加/更改/查询/删除。

So in Inquiry/Delete modes I wouldn't be using form or input fields.. 所以在查询/删除模式下,我不会使用表格或输入字段。

I then came across an issue in that ng-container wasn't smart enough to realise I was conditioning my form statement in two separate statements with input fields in between. 然后,我遇到了一个问题,即ng-container不够智能,无法意识到我在两个单独的语句中使用两个输入字段之间的条件来调节form语句。

This code caused a template parser error: 此代码导致模板解析器错误:

<ng-container *ngIf="useForm()">
  <form [formGroup]="gvarForm" 
        novalidate
        (ngSubmit)="submit()">
</ng-container>
....
<ng-container *ngIf="useForm()">
</form>    
</ng-container>

The error: 错误:

Runtime Error
Template parse errors: Unexpected closing tag "ng-container". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags (" novalidate (ngSubmit)="submit()"> [ERROR ->]</ng-container> <ng-container *ngIf="useForm()"> </form> "): ng:///SharedModule/GvarDetailPage.html@30:6 Unexpected closing tag "form". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags (" </ng-container> <ng-container *ngIf="useForm()"> [ERROR ->]</form> </ng-container> </ion-content> "): ng:///SharedModule/GvarDetailPage.html@32:6 Unexpected closing tag "ion-content". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags (" </form> </ng-container> [ERROR ->]</ion-content> <ion-footer> <ion-toolbar> "): ng:///SharedModule/GvarDetailPage.html@34:0
Stack
Error: Template parse errors:
Unexpected closing tag "ng-container". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
              novalidate
              (ngSubmit)="submit()">
      [ERROR ->]</ng-container>
      <ng-container *ngIf="useForm()">
      </form>    
"): ng:///SharedModule/GvarDetailPage.html@30:6
Unexpected closing tag "form". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
      </ng-container>
      <ng-container *ngIf="useForm()">
      [ERROR ->]</form>    
      </ng-container>
</ion-content>
"): ng:///SharedModule/GvarDetailPage.html@32:6
Unexpected closing tag "ion-content". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
      </form>    
      </ng-container>
[ERROR ->]</ion-content>
<ion-footer>
  <ion-toolbar>
"): ng:///SharedModule/GvarDetailPage.html@34:0
    at syntaxError (http://localhost:8100/build/vendor.js:79188:34)
    at DirectiveNormalizer.normalizeLoadedTemplate (http://localhost:8100/build/vendor.js:91005:19)
    at DirectiveNormalizer.normalizeTemplateSync (http://localhost:8100/build/vendor.js:90981:21)
    at DirectiveNormalizer.normalizeTemplate (http://localhost:8100/build/vendor.js:90955:43)
    at CompileMetadataResolver._loadDirectiveMetadata (http://localhost:8100/build/vendor.js:91893:75)
    at http://localhost:8100/build/vendor.js:92089:54
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver.loadNgModuleDirectiveAndPipeMetadata (http://localhost:8100/build/vendor.js:92088:41)
    at http://localhost:8100/build/vendor.js:103284:58
    at Array.forEach (<anonymous>)

But this does not: 但这不是:

<ng-container *ngIf="useForm()">
  <form [formGroup]="gvarForm" 
        novalidate
        (ngSubmit)="submit()">
   </form>    
</ng-container>

This makes me think I'm going to have to rewrite the thing and can't use the DRY principle. 这使我认为我将不得不重写该东西并且不能使用DRY原理。

Anyone else come across this? 还有其他人遇到吗?

Well if you are using those input elements inside your form only, then why to keep them separate? 好吧,如果仅在表单中使用这些输入元素,那么为什么要将它们分开?

<ng-container *ngIf="useForm()">
  <form [formGroup]="gvarForm" 
        novalidate
        (ngSubmit)="submit()">

     ... your input element goes here

   </form>    
</ng-container>

On separate note: If your form is required in multiple places and you don't want to repeat them You can create your form as a separate component and include it whenever required. 另请注意: 如果您的表单需要在多个地方,并且您不想重复它们,则可以将表单创建为单独的组件,并在需要时将其包括在内。

Example: 例:

<ng-container *ngIf="useForm()">
  <my-form></my-form>

  .... some other code

  <my-form></my-form>

  .... some other code    
</ng-container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM