[英]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>
If there no value selected in dropdown
i'm getting below error :( TypeError: Cannot read property 'name' of undefined" 如果no value selected in dropdown
我将出现以下错误:( TypeError:无法读取未定义的属性“名称””
js code Name: $scope.property.Name.Name js代码名称:$ scope.property.Name.Name
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 但是我想如果仍然没有选择任何值,我可以检查undefined或null,以便我的Json可以设置值并执行代码而无需执行
Use some other model variable instead of property
. 使用其他模型变量代替property
。 Here in the modified HTML I have used selectedProperty
在这里,在修改后的HTML中,我使用了selectedProperty
<select id="ddlPropertyName"
ng-options="property.Name for property in allProperty"
ng-model="selectedProperty">
<option value="">-Select Name-</option>
</select>
Note: You are using label for value in array
in ngOption so selectedProperty
will hold the property
type 注意:您正在ngOption中使用label for value in array
中label for value in array
因此selectedProperty
将保存property
类型
Below Code Works For Me 以下代码对我有用
<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";
}
}
This error occurs basically when the fields specified by you in the Column Definition doesn't have name or the same column has been put twice in the Column Definition. 当您在“列定义”中指定的字段没有名称或同一列两次被放入“列定义”时,基本上会发生此错误。 Search for the similar column and remove one with the same name that will solve this error. 搜索相似的列,并删除具有相同名称的列,以解决此错误。
Just use below code in your controller 只需在控制器中使用以下代码
if($scope.property.Name !== null || $scope.property.Name !== undefined) {
console.log('here');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.