简体   繁体   English

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>

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 arraylabel 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.

相关问题 TypeError:无法读取angularJS中未定义的属性“名称” - TypeError: Cannot read property 'name' of undefined in angularJS 错误TypeError:无法读取未定义的属性&#39;名称&#39;-使用angularjs - ERROR TypeError: Cannot read property 'name' of undefined - Using angularjs 编辑表单的下拉列表时,我遇到了这个问题:TypeError: Cannot read property &#39;name&#39; of undefined - When editing dropdown of form I've got this issue: TypeError: Cannot read property 'name' of undefined 类型错误:无法使用 AngularJS 读取未定义的属性“推送” - TypeError: Cannot read property 'push' of undefined with AngularJS AngularJS:TypeError:无法读取未定义的属性“childNodes” - AngularJS : TypeError: Cannot read property 'childNodes' of undefined angularJS)TypeError:无法读取未定义的属性“ push” - angularJS) TypeError: Cannot read property 'push' of undefined TypeError:无法读取angularjs中未定义的属性“ reduce” - TypeError: Cannot read property 'reduce' of undefined in angularjs AngularJS - TypeError:无法读取未定义的属性'go' - AngularJS - TypeError: Cannot read property 'go' of undefined Angularjs / Ionic TypeError:无法读取未定义的属性&#39;then&#39; - Angularjs/Ionic TypeError: Cannot read property 'then' of undefined TypeError:无法读取AngularJS上未定义的属性“ get” - TypeError: Cannot read property 'get' of undefined on AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM