Change one single iteration on an ng-repeat

2019-06-15 07:55发布

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>

6条回答
\"骚年 ilove
2楼-- · 2019-06-15 08:06

Like other's said, write your own filter.

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

    return function(country) {
        return country.replace(/^(\S)\S*(\s+\S+)/m, "$1.$2");

    }

});

Example:

alert("Northern Ireland".replace(/^(\S)\S*(\s+\S+)/m, "$1.$2"));
alert("South Africa".replace(/^(\S)\S*(\s+\S+)/m, "$1.$2"));
alert("USA".replace(/^(\S)\S*(\s+\S+)/m, "$1.$2"));

查看更多
Emotional °昔
3楼-- · 2019-06-15 08:17

You can try this. It is simple.

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

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

  <md-grid-tile-footer>
    <H3 ng-if="nation.name.split(' ').length > 1">{{ nation.name | limitTo:1 | uppercase }}. {{ nation.name.split(' ')[1] }}</H3>
    <H3 ng-if="nation.name.split(' ').length == 1">{{ nation.name }}</H3>
  </md-grid-tile-footer>

</md-grid-tile>
查看更多
祖国的老花朵
4楼-- · 2019-06-15 08:22

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;

    }

});
查看更多
Anthone
5楼-- · 2019-06-15 08:24

use angular

https://angular-translate.github.io/

then you put all your country names as keys, and you will use the translate filter

{{nation.name | translate }}

and you can add for example for english language : en.json :

[
  {
    "Italy": "Italy",
    "Northern Ireland": "N.Ireland",
    "Poland": "Poland",
.....
  }
]
查看更多
霸刀☆藐视天下
6楼-- · 2019-06-15 08:25

You can create a custom filter to achieve this, as shown below:

In HTML do this :

<h3>{{nation.name | myFormat}}</h3>

In your js, define this filer as below:

app.filter('myFormat', function() {
    return function(x) {
        x = x.split(" ");
        if(x.length>1){
            x = x[0].split("")[0] + ". " + x[1];
            return x;
        }
        else
            return x[0];
    };
});

Please not that as per your req the custom filter can be modified and we can use for loops if you got more than 2 words to be handled as well.

查看更多
Animai°情兽
7楼-- · 2019-06-15 08:25

For a simple truncate you can use the limitTo filter and it makes sense to keep the model unchanged.

Here a simple example (8 chars):

<md-grid-tile ng-repeat="nation in nationData">
  <img src="img/{{nation.name}}.png">

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

</md-grid-tile>
查看更多
登录 后发表回答