1

I have a list of countries (with codes and names) and when the list is unfolded I want to display "code + name" (for example "+44 United Kingdom") but when we select one country, only display the code ("+44"). Is possible to do with Angular ngOptions?

<select ng-model="userData.phoneCode" ng-options="(c.countryCallingCode.replace('00', '+') + ' ' + c.countryName) for c in countries track by c.countryId">
        <option value="" disabled selected data-i18n="Country"></option>
</select>

Format of country list in the controller:

countries = [
    {
      "countryCallingCode":"0033",
      "countryCode":"FR",
      "countryId":123,
      "countryName":"France"
    },
]

I want this:

enter image description here

I have been searching in google and I tried to create a Directive for this (my level in angularjs is medium) but I don't find a solution, always display the selected option as "code + name".

Any idea?

5
  • Could you show the country list? (I mean the array of objects in your controller) Commented Oct 31, 2016 at 11:02
  • @LenilsondeCastro I edit my post with the country list Commented Oct 31, 2016 at 11:13
  • I've tested your code and it's working fine check this out jsfiddle.net/tt27bjsn/1 Commented Oct 31, 2016 at 11:18
  • Check the image that I just added it in my post Commented Oct 31, 2016 at 11:31
  • Umm, it seems to be a OSX issue, because it looks ok on windows (firefox and chrome). Commented Oct 31, 2016 at 11:41

3 Answers 3

2

I always use alias, "as", in ng-options:

<select ng-model="userData.phoneCode" ng-options="c.countryId as (c.countryCallingCode.replace('00', '+') + ' ' + c.countryName) for c in countries track by c.countryId">
    <option value="" disabled selected data-i18n="Country"></option>

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

Comments

0

// Code goes here

angular
  .module('myApp', [])
  .run(function($rootScope) {
    $rootScope.title = 'myTest Page';
  })
  .controller('testController', ['$scope',
    function($scope) {
      $scope.countries = [{
        "countryCallingCode": "0033",
        "countryCode": "FR",
        "countryId": 123,
        "countryName": "France"
      }, ]
    }
  ])
<!DOCTYPE html>
<html data-ng-app="myApp">

<head>
  <link rel="stylesheet" href="style.css">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
  <script src="script.js"></script>
  <style>
    .my-class {
      width: 51px !important;
    }
  </style>
</head>

<body data-ng-controller="testController">



  <select class="form-control my-class" name="mySelect" id="mySelect" ng-options="c.countryId as c.countryCallingCode.replace('00', '+')+'   '+c.countryName for c in countries" ng-model="userData.phoneCode">
    <option value="" disabled selected data-i18n="Country"></option>
  </select>



  selected : {{userData.phoneCode}}
</body>

</html>

You might need some styling adjustments then.

5 Comments

Hi, Thanks but that is not I wanted it. I just edited my post for add a image. I want to display only the code in the selected option and when the list is unfolded, display code + name. I don't know if is possible to do with Angularjs.
@josekron, You might need some styling adjustments then
@Niknil I tried to give it a specific width for hiding " France" and only show "+33" but the problem is there are countries with a one-digit codes or three-digit codes. I could use a CSS stylesheet to dynamically adjust the width for the different digit size but I think it won't look professional... I prefer to put the same width with one, two or three digits.
@josekron, Thanks for the interesting task,Will try to achieve this.
@Niknil I found a solution although it's not very sophisticated. See my answer.
0

I found a solution that works for me although maybe it's not the best solution...

HTML:

<div style="width:60px">
<select ng-model="userData.phoneCode" style="white-space:pre-wrap;" ng-options="(getFormatCountryCode(c)) for c in countries track by c.countryId">
    <option value="" disabled selected data-i18n="Country"></option>
</select>
</div>

Controller:

$scope.getFormatCountryCode = function(c){

  var code = c.countryCallingCode.replace('00', '+');
  var spaces = '   ';
  var sizeCode = code.length;
  if(sizeCode == 3){
    spaces += '    ';
  }
  else if(sizeCode == 2){
    spaces += '     ';
  }
  return code +spaces+c.countryName;
}

enter image description here enter image description here

In the function "getFormatCountryCode" I add the white-spaces depending on the length of each code. AngularJs won't display the white-spaces in the unfolded list but adding the style "white-space:pre-wrap;" Angularjs will display the white-spaces in the selected option. So we only have to put a specific width for hide the name.

Comments

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.