Filter uppercase with ng-options
Asked Answered
I

4

10

I didn't find how to uppercase or uppercase first letter in ng-options.

My select:

<select ng-model="company.currency.code" ng-options="currency.code as currency.code for currency in currency_list>
</select>

In controller:

$scope.currency_list = [
    {
        code: 'eur'
    }, {
        code: 'usd'
    }
];

I'd like to print "EUR", "USD", or "Eur", "Usd" without looping manually my object.

Is that possible to do this?

Iridize answered 30/9, 2014 at 14:55 Comment(0)
N
14

This should work:

ng-options="currency.code as (currency.code | uppercase) for currency in currency_list"

See the filter docs: https://docs.angularjs.org/api/ng/filter/uppercase

Neurotic answered 30/9, 2014 at 15:0 Comment(0)
C
4

Use uppercase filter.

Take a look at this

var app = angular.module('myApp', []);
app.controller('ArrayController', function ($scope) {
    $scope.currency_list = [
    {
        code: 'eur'
    }, {
        code: 'usd'
    }
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp' ng-controller="ArrayController">
    <select ng-model="company.currency.code" ng-options="currency.code as (currency.code | uppercase) for currency in currency_list | uppercase">
</select>
</div>
Condone answered 30/9, 2014 at 15:2 Comment(1)
Like those inline-runnable code a lot! It took me some googling to find the recent blog entry about runnable code (blog.stackoverflow.com/2014/09/… ). Neat. Will do that too, in the future.Neurotic
E
1

You can easily use a css approach for this by attaching a class to the

var app = angular.module('myApp', []);
app.controller('ArrayController', function ($scope) {
    $scope.currency_list = [
      {
          code: 'eur'
      }, {
          code: 'usd'
      }
    ];
});
.text-capitalize{ text-transform: capitalize }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp' ng-controller="ArrayController">
    <select 
      class="text-capitalize"
      ng-model="company.currency.code"
      ng-options="currency.code as currency.code for currency in currency_list">
    </select>
</div>
Eleemosynary answered 6/4, 2018 at 9:46 Comment(0)
G
0

You can use this attribute:
ng-options="currency.code | uppercase for currency in currency_list"

Gennie answered 18/11, 2020 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.