![](/img/trans.png)
[英]TypeError: Cannot read property 'name' of undefined in angularJS
[英]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:无法读取未定义的属性“名称””
js代码名称:$ scope.property.Name.Name
但是我想如果仍然没有选择任何值,我可以检查undefined或null,以便我的Json可以设置值并执行代码而无需执行
使用其他模型变量代替property
。 在这里,在修改后的HTML中,我使用了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
中label for value in array
因此selectedProperty
将保存property
类型
以下代码对我有用
<select ng-model="qid">
<option value="">--Select--</option>
<option ng-repeat="p in pollquestions" value="{{p.QId}}">{{p.QText}}</option>
</select>
app.controller("optionsCntrl", function ($scope, angularService) {
//$scope.optionsCntrl = [{ QId: 1, QText: "What is this 1?" }];
var getData = angularService.getQuestion();
getData.then(function (ques) {
$scope.pollquestions = ques.data;
}, function () {
alert('Error in getting records');
});
$scope.AddUpdatePollQuestionOption = function () {
var pollquestionanswer = {
qid:$scope.qid,
anstext: $scope.anstext,
anscount: 1
};
var getData = angularService.addPollQuestionOptions(pollquestionanswer).success(deferred.resolve).error(deferred.reject);
getData.then(function (msg) {
alert(msg.data);
}, function () {
alert('Error in adding record');
});
}
});
public string AddPollQuestionOptions(PollQuestionAnswer pollQuestionAnswer)
{
if (pollQuestionAnswer != null)
{
if (!String.IsNullOrEmpty(Convert.ToString(pollQuestionAnswer.qid)))
{
try
{
using (DemoContext contextObj = new DemoContext())
{
contextObj.pollquestionanswer.Add(pollQuestionAnswer);
contextObj.SaveChanges();
return "Poll Question Answer Added";
}
}
catch (Exception objEx)
{
return "Poll Question Not Found";
}
}
else
{
return "Invalid Request";
}
}
else
{
return "Invalid Record";
}
}
当您在“列定义”中指定的字段没有名称或同一列两次被放入“列定义”时,基本上会发生此错误。 搜索相似的列,并删除具有相同名称的列,以解决此错误。
只需在控制器中使用以下代码
if($scope.property.Name !== null || $scope.property.Name !== undefined) {
console.log('here');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.