Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change one single iteration on an ng-repeat

I have an ng-repeat that iterates through the names of countries in my model. On certain country names I want them abbreviated to reduce the length of the string, for example, I want 'Northern Ireland' to be outputted as 'N. Ireland'.

JSON Model

[
  {
    "id": 1,
    "name": "Italy",
  },

  {
    "id": 2,
    "name": "Northern Ireland",
  },

  {
    "id": 3,
    "name": "Poland",
  }
]

I could just change the name in my model, but I'd rather leave that as it is as the I want the raw data to be complete. It is only in this specific instance I want to have it abbreviated.

Should I use an ng-repeat filter? If so, how?
If not, any other suggestions?

HTML

<md-grid-tile ng-repeat="nation in nationData">

  <img src="img/{{nation.name}}.png">

  <md-grid-tile-footer>
    <h3>{{nation.name | uppercase}}</h3>
  </md-grid-tile-footer>

</md-grid-tile>
like image 381
Paulos3000 Avatar asked Apr 06 '16 13:04

Paulos3000


People also ask

How to iterate over the keys and values with ng-repeat in AngularJS?

- GeeksforGeeks How to iterate over the keys and values with ng-repeat in AngularJS ? The task is to iterate over a JS object (its keys and values) using the ng-repeat directive. This can be done using parenthesis in the ng-repeat directive to explicitly ask for a key-value pair parameter from angularJS.

How to iterate over a list with ng-repeat filter?

Now you want to iterate over this subset of the list, after clicking an assigned button and change a property on the items that match this filter. To iterate over a collection of objects already filtered by ng-repeat filters we use an alias expression.

How to iterate over a JS object using the ng-repeat directive?

The task is to iterate over a JS object (its keys and values) using the ng-repeat directive. This can be done using parenthesis in the ng-repeat directive to explicitly ask for a key-value pair parameter from angularJS. Here the variable key contains the key of the object and value contains the value of the object.

How to iterate through the filtered students in AngularJS?

Now, inside that AngularJS function, as the alias variable is containing values of only those objects which got filtered, so we iterate over each of those values. For each iteration, we change some property like in this case the marks of the filtered students by 10 and then console the values.


1 Answers

You could create your own filter abbreviate that is applied to the name. In this filter you could switch on the country name and return the abbreviated format.

app.filter('abbreviate', function() {

    return function(country) {

        switch(country){
            case "Northern Ireland":
                country = "N. Ireland"
                break;
        }

        return country;

    }

});
like image 134
Alex Logan Avatar answered Oct 06 '22 23:10

Alex Logan