Html 无法读取角度 2 中未定义的属性“错误”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38028388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot read property 'errors' of undefined in angular 2
提问by MMR
I am validating my template using angular2, but meanwhile it shows this error:
我正在使用 angular2 验证我的模板,但同时它显示了这个错误:
Cannot read the property 'errors' of undefined.
Here is my template:
这是我的模板:
<h3 class = "head">{{title}}</h3>
<form [ngFormModel]="form" #f="ngForm">
<div class="row">
<div class="form-group">
<label class="formHeading">Facebook</label>
<input type="text" id="facebook" class="form-control col-xs-3" ngControl="facebook" #facebook="ngForm" >
</div>
<div *ngIf ="facebook.touched && facebok.errors">
<div class="form-row btn">
<button type="submit" class="btn btn-primary pull-right butspace" [disabled]="!f.valid">Save</button>
</div>
</div>
</form>
I dont know why it shows this error. Can anyone fix it?
我不知道为什么它会显示此错误。任何人都可以修复它吗?
回答by hholtij
First of all, you have facebok
instead of facebook
on the error property.
首先,您拥有facebok
而不是facebook
错误属性。
If that doesn't fix it you're probably using the facebook
object before it is assigned, which can happen if it's an @Input()
for example.
如果这不能解决它,您可能facebook
在分配之前使用了该对象@Input()
,例如,如果它是一个,则可能会发生这种情况。
Use the Elvis operator
:
使用Elvis operator
:
*ngIf ="facebook?.touched && facebook?.errors"