[英]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.