Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

javascript - Cannot read property $scope.dropdownfield of null in angularjs

I have a simple registration form page. The form page have a person_id field and drop down list name condition_code_1. Person_id field is mandatory field meanwhile the condition_code_1 drop down list is an optional field mean's that it is optional for user to be selected or not. The value of the drop down list is fetched through specialConds array that data are get from oracle db field.

I stored the value of drop down list in data array in Condition_code_1 controller.The problem is when i do not select the list and submitted the form, angularjs throws an error : Cannot read property 'condCode1' of null. How do i ignored the drop down list when it is not selected. Any help here.

1) Condition_code_1 list

  <select id = "condition_code_1"
          data-placeholder = "Please select"   
          ng-model         = "specialCond.condCode1"
          ng-options       = "t.condCode as t.condDesc for t in specialConds" chosen>
          <option value="">&nbsp;</option>
  </select>

2) Condition_code_1 Controller

 $scope.create = function () {
    var data = {};

    data.personId = $scope.person_id;
    data.conditionCode1 = $scope.specialCond.condCode1; 

    CondCodeSvc.create(data, function(res){
           jAlert('Create successfull');
    }, errSvc.errorHandler);                   
 };
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's because data doesn't exist before you call $scope.create(), therefore is undefined, which means is not an object, which means it cannot have a property, thus javascript throws the error.

try:

var data = {};
$scope.create = function () {

    data.personId = $scope.person_id;
    data.conditionCode1 = $scope.specialCond.condCode1; 

    CondCodeSvc.create(data, function(res){
       jAlert('Create successfull');
    }, errSvc.errorHandler);                   
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...