0

It's possible to make for exmple :

<p>{{data.country}}</p> print out Country:New York, how to alter that to Country:NY ?

4
  • 1
    fyi i've never heard of the country new york. Commented Jun 20, 2015 at 12:32
  • do you have the mapping from the full name to abbreviation? Commented Jun 20, 2015 at 12:32
  • @DanielA.White I don't need that, I just need a simple string alter in the view. Commented Jun 20, 2015 at 12:34
  • @AaronMusktin I think the way you want..I had implemented in the same way.. :) Commented Jun 20, 2015 at 13:14

4 Answers 4

1

You could do it using Regx .match and strings join method.

Markup

 {{data.country.match(pattern).join('')}}

Controller

app.controller('MainCtrl', function($scope, $http) {
$scope.data = {};
  $scope.data.country = 'New York';
  $scope.pattern = /\b(\w)/g;
});

Demo Plunkr

Sign up to request clarification or add additional context in comments.

3 Comments

ah this is way too complicated.
But the solution of pankajparkar, is better
@AaronMusktin I don't think so its hard..If you want to increase its readability then you could move this code to custom filter as wZVang did..
1

Create a custom filter:

.filter("countryCaps", function(){
  return function(country){
    return "Country: " + country.match(/[A-Z]/g).join("")
  }
})
<p>{{data.country | countryCaps}}</p>

Demo

Comments

0

Always remember that what you have inside the {{...}} is an AngularJS expression. That means you can call functions as well.

Just put a function on your scope like this:

$scope.countryTranslator = function (country) {
  if (country === 'New York') {
    return 'NY';
  }
};

and call it like this instead:

{{ countryTranslator(data.country) }}.

Obviously your "translator" should have better code than my example, but you get the idea.

2 Comments

This is bad idea, to use $scope and storage for your custom functions accessible from templates. Read github.com/johnpapa/angular-styleguide
That is certainly one person's opinion of a bad idea. I have read his guide and there are certainly areas where there is some room for opinion. For example, embedding all your components in immediately executed functions? Or controllerAs with vm?
-1

You should use custom filter for this purpose.

But you still have a problem with database of cities full name and they shortcuts, for this purpose, you can use static json file, or use side server service in your choice.

4 Comments

Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
Custom filter is one of the way to do it, I'm asking it's possible to do it with view, I know how to achieve that in the model layer too.
@AaronMusktin don't do that, really. View should present info not to alter it, you should format it in any way in model. I guess you should read code style too github.com/johnpapa/angular-styleguide
@DanielA.White everything matters, he could not use filters if he didn't read that article. Should I copy paste full article there?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.