简体   繁体   中英

With Angular - How to Pre-Select a Option that gets repeated out if a value exist?

So what I am trying to do is pass a parameter in the url to a page with a select being repeated out. The parameter passing is customerProfile.id. Then I am trying to find that customerProfile.id in the select options and select it. How do I go about that? So if customerProfile.id exist select corresponding option if not default. The ids being spit out are all like 1,2,3,4,5, etc... If you need more info let me know.

Here is the select:

<div class="form-group" id="testpick">
   <label for="field_customerProfile">customerProfile</label>
   <select class="form-control" id="field_customerProfile" name="customerProfile"
     ng-model="rentalAgreement.customerProfile"
     ng-options="customerProfile as customerProfile.lastName for customerProfile in customerprofiles track by customerProfile.id">
      <option value=""></option>
   </select>
</div>

Tried this with the 1 being replaced later with the parameter.

angular.element('div#testpick select').val('1');

In the ng-model you could store only the customer id instead of the whole customer object, ie replace ng-options="customerProfile ... with ng-options="customerProfile.id ... :

<select class="..." id="..." name="..." ng-model="rentalAgreement.customerProfileId" ng-options="customerProfile.id as customerProfile.lastName for customerProfile in customerprofiles track by customerProfile.id">
    <option value=""></option>
</select>

Then the idea is to set the variable binded to the ng-model , ie rentalAgreement.customerProfile , to the parameter value from the url, which is the customer id. If the setted value matches one of the options, the select list will point on that value. You could retrieve the url parameter by injecting $location in the controller, and look into $location.search() :

Controller

Assuming the url parameter is named customerId :

$scope.rentalAgreement = {
    customerProfileId: $location.search().customerId
}

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