1

I had a label containing value user.Rules e.g . London

<label id="ruleId" for="Rules" ng-model="user.Rules" ng-hide="editmode"
    style="width:97%; word-wrap:break-word; overflow-wrap:break-word;">{{user.Rules }}
</label>

After click on edit button a drop down list appears containing the list of states e,g Delhi, Pune, London etc.,

<select class="form-control" name="user.Rules" data-ng-model="user.Rules" ng-options="option for option in nestedList" ng-show="editmode" style="width:100%; ">
    <option value="" style="margin-left:25px">-Select Rule-</option>
</select>

I need to set the selected value as the label value of drop down list i.e. London

How can I do that ?

2
  • What is the propertyname of 'the option' that you want to display? Can you show us what nestedList looks like in your controller? Commented May 18, 2015 at 4:58
  • here's some example code using select and ng-options : link hope this helps. Commented May 18, 2015 at 5:05

1 Answer 1

1

Since there is no sample code and sample data for nestedList is not available, so I assume the data as my own and created this sample.

I consider the $scope.RuleId contains the id from the database. I removed the ng-model="user.Rules" from the label and and based on the $scope.RuleId I find its equivalent value.

HTML Code:

<div ng-controller="MyCntrl">
    <label id="ruleId" for="Rules" ng-hide="editmode" 
    style="width:97%; word-wrap:break-word; overflow-wrap:break-word;">{{selectedLabel}}
    </label>
    <select class="form-control" name="ruleDetails" data-ng-model="RuleId"
        ng-options="option.RuleId as option.Rules for option in nestedList"
        ng-show="editmode" style="width: 100%;">
        <option value="" style="margin-left:25px">-Select Rule-</option>
    </select>
    <div style="height: 10px"></div>
    <div>
        <button ng-click="editButton()">Edit</button>
    </div>
</div>

Controller Code:

function MyCntrl($scope) {
  $scope.editmode = false;
  $scope.RuleId = "001";

  $scope.nestedList = [{
    "Rules": "London",
    "RuleId": "001"
  }, {
    "Rules": "Delhi",
    "RuleId": "002"
  }, {
    "Rules": "Pune",
    "RuleId": "003"
  }, {
    "Rules": "Mumbai",
    "RuleId": "004"
  }, {
    "Rules": "Chennai",
    "RuleId": "005"
  }];

  angular.forEach($scope.nestedList, function(rule) {
    if (rule.RuleId === $scope.RuleId) {
      $scope.selectedLabel = rule.Rules;
    }
  });

  $scope.editButton = function() {
    $scope.editmode = true;
  };
}

The same code is added in the Working Sample for your reference.

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

Comments

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.