我刚接触angular,我遇到了这样的问题:
当我在元素中放入一个ngIf时,
 <input type="text" *ngIf="true" id="name" name="name" class="form-control" required [(ngModel)]="name" #name="ngModel" />
aaaaaa {{name.dirty}}我得到了
ERROR TypeError: Cannot read property 'dirty' of undefined
at Object.eval [as updateRenderer] (PrepareInvestmentComponent.html:1)
at Object.debugUpdateRenderer [as updateRenderer] (core.es5.js:13105)
at checkAndUpdateView (core.es5.js:12256)
at callViewAction (core.es5.js:12599)
at execComponentViewsAction (core.es5.js:12531)
at checkAndUpdateView (core.es5.js:12257)
at callViewAction (core.es5.js:12599)
at execEmbeddedViewsAction (core.es5.js:12557)
at checkAndUpdateView (core.es5.js:12252)
at callViewAction (core.es5.js:12599)但是,当我删除ngIf时,它工作得很好。我能知道为什么和任何迹象来解决这个问题吗?
发布于 2018-02-13 17:42:09
似乎angular不允许您使用引用变量+ *ngIf...the解决方案是使用ng-container或仅使用div
像这样:https://stackblitz.com/edit/angular-48763586-rsrtsa?file=app/app.component.html
有一个问题似乎与您的情况有关:https://github.com/angular/angular/issues/13974
发布于 2018-02-13 17:46:59
如果您想检查输入元素是否为脏元素,请尝试此操作
<input type="text"  id="name" name="name" class="form-control" required 
[(ngModel)]="name" #n="ngModel" />
<p *ngIf="n.dirty">{{n.dirty}}</p>发布于 2018-02-13 17:50:31
您正在为#name="ngModel"签名。模板变量类似于元素的标识符。你没有给他们任何任务。此外,您已经在组件中定义了一个名称,因为您正在[(ngModel)]定义中使用它。
此外,您还可以在使用ReactiveForms时访问dirty。看一下这段代码。它的工作方式可能与您预期的一样。
https://stackoverflow.com/questions/48763586
复制相似问题