AngularJS-TypeError:无法读取未定义的属性“名称"使用下拉菜单时 [英] Angularjs - TypeError: Cannot read property 'name' of undefined" when work with dropdown
问题描述
<select id="ddlPropertyName" ng-options="property.Name for property in allProperty" ng-model="property.Name"><option value="">-Select Name-</option>
</select>
如果存在no value selected in dropdown
,我将遇到以下错误:(
TypeError:无法读取未定义的属性名称""
If there no value selected in dropdown
i'm getting below error :(
TypeError: Cannot read property 'name' of undefined"
js代码 名称:$ scope.property.Name.Name
js code Name: $scope.property.Name.Name
但是我想如果仍然没有选择任何值,我可以检查undefined或null,以便我的Json可以设置值并执行代码而无需执行
But I want if no value gets selected still I can check undefined or null so that my Json can set value and code execute without execption
推荐答案
使用其他一些模型变量代替property
.在这里,在修改后的HTML中,我使用了selectedProperty
Use some other model variable instead of property
. Here in the modified HTML I have used selectedProperty
<select id="ddlPropertyName"
ng-options="property.Name for property in allProperty"
ng-model="selectedProperty">
<option value="">-Select Name-</option>
</select>
注意:您正在 ngOption 中使用label for value in array
,所以selectedProperty
将保留property
类型
Note: You are using label for value in array
in ngOption so selectedProperty
will hold the property
type
这篇关于AngularJS-TypeError:无法读取未定义的属性“名称"使用下拉菜单时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!