1

I am unable to set value dynamically into drop down list using Angular.js. Here is my code:

<div class="input-group bmargindiv1 col-md-12">
    <span class="input-group-addon ndrftextwidth text-right" style="width:180px">Business Name :</span>
    <select class="form-control" id="restau" ng-model="subcat" ng-options="qua.name for qua in listOfRestaurant | orderBy:'name' track by qua.value" ng-change="getDayFromSpecial('restau');">
      <option value="">Select Business Name</option>
    </select>
  </div>
  <input type="button" id="btn" value="Open" ng-click="openGallery();">
   <input type="button" id="btn1" value="select" ng-click="openSelect();">

My controller side code is given below.

$scope.openSelect=function(){
     $scope.subcat.value="1";
   }

Here I am trying to set value using button click but it's throwing error. Here is my plunkr code.

1 Answer 1

2

Because you haven't define $scope.subcat object yet & you're trying to assign value property of it.

$scope.subcat = {}; //define it inside controller
$scope.openSelect=function(){
   $scope.subcat.value = "1";
}

Forked Plunkr

Warning: as you're only setting value property of object, ng-options would select option for you in dropdown. But what about other property values. If you are assuming value as unique constraint & totally responsible entire record identity then its completely fine.

Sign up to request clarification or add additional context in comments.

2 Comments

@ Pankaj : Here is one problem i could not get the set of data in console while click on open button.
@satya so you have to set whole object to $scope.subcat inspite of just value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.