简体   繁体   中英

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"

js code Name: $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

Use some other model variable instead of property . Here in the modified HTML I have used 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

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM