angularjsangularjs-ng-repeatangularjs-ng-optionsui-select2

Mapping ng-model with corresponding ng-value from json data for select


I have a select element like this

<select class="span3" ui-select2="{minimumResultsForSearch: -1}" ng-model="envelope.roofType">
            <option value="">Select..</option>
            <option ng-value="roof.id" ng-repeat="roof in roofList">{{roof.type}}</option>
          </select>

So basically i'm sending the id corresponding to roofType in my post call.Now in my get call i get the same id back.I'm doing this in my controller.

 $scope.envelope = success.records;

Now in my view since ng-model corresponds to envelope object,envelope.roofType is showing the id instead of actual type. My question is,is there any way to map these id to actual text that should be displayed?


Solution

  • If you want to get the element that corresponds to that id, you can do it with a simple find function:

    $scope.envelope = roofList.find(function(element) {
      return element.id == success.records ;
    });
    

    This will return the element of the array that corresponds to the id, I'm supposing success.records is the id that you wanna map.