Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Angular js ngOptions with custom attributes [duplicate]

I want to populate a dropdown list with a few custom values using Angular.js ngOptions

Eg:-

<option value="' + info[x].CountryCode 
                 + '"   data-image="images/msdropdown/icons/blank.gif" 
        data-imagecss="flag ' + info[x].CountryCode 
                              + '" data-title="'
                              + info[x].CurrencyName + '" >' 
                              + info[x].CurrencyCode + '
</option>

Following is the data in JSON format :

[{
    "Id": 3,
    "CountryName": "Australia",
    "CountryCode": "au",
    "CurrencyCode": "AUD",
    "CurrencyName": "Australian Dollar",
    "CurrencyValue": 0.018,
    "PayPalCountryCode": 12,
    "PayPalCurrencyCode": 78,
    "IsActive": true
  },
  {
    "Id": 19,
    "CountryName": "Taiwan",
    "CountryCode": "tw",
    "CurrencyCode": "TWD",
    "CurrencyName": "New Taiwan Dollar",
    "CurrencyValue": 0.48,
    "PayPalCountryCode": 208,
    "PayPalCurrencyCode": 148,
    "IsActive": true
  },
  {
    "Id": 2,
    "CountryName": "United States",
    "CountryCode": "us",
    "CurrencyCode": "USD",
    "CurrencyName": "US Dollar",
    "CurrencyValue": 0.016,
    "PayPalCountryCode": 225,
    "PayPalCurrencyCode": 125,
    "IsActive": true
  }]

I am new to Angular, could someone show me how to do this using ngOptions ?

like image 594
Monodeep Avatar asked Dec 20 '25 12:12

Monodeep


1 Answers

As per Angular ng-options documentations there are no custom attributes support. so use ng-repeat directive to achieve your select box

<select ng-model="countrySelected" ng-change="countryChange()">
  <option ng-repeat="country in countries" value="{{country.CountryCode}}" 
          data-image="images/msdropdown/icons/blank.gif" 
          data-imagecss="flag  {{country.CountryCode}}" 
          data-title="{{country.CurrencyName}}">{{country.CurrencyCode}}</option>
</select>

you get the selected value through ng-model. countrySelected stores your selected value.

in your controller

$scope.countryChange = function(){

// write your related code
}
like image 73
chandu Avatar answered Dec 23 '25 03:12

chandu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!