Solved

AngularJS Country select directive

Posted on 2014-07-30
4
1,693 Views
Last Modified: 2014-09-09
I'm trying to add the country codes to this AngularJS Country Select directive:
https://github.com/banafederico/angularjs-country-select

However, I'm not sure how to write the template to add the code as the option value:

    // shortening countries list
    var countries = {"AD":"Andorra", "AE":"United Arab Emirates", "AF":"Afghanistan", "AG":"Antigua and Barbuda", "AI":"Anguilla", 
                     "AL":"Albania", "AM":"Armenia", "AN":"Netherlands Antilles", "AO":"Angola", "AQ":"Antarctica", "AR":"Argentina", 
                     "AS":"American Samoa", "AT":"Austria", "AU":"Australia", "AW":"Aruba", "AX":"Åland Islands", "AZ":"Azerbaijan"};

    return {
      restrict: 'E',
      // How do I change this template to have the code as option value?
      template: '<select><option>' + countries.join('</option><option>') + '</option></select>',
      replace: true,
      link: function (scope, elem, attrs) {
        // How do I modify these to use the code and not the country name?
        if (!!attrs.ngModel) {
          var assignCountry = $parse(attrs.ngModel).assign;

          elem.bind('change', function (e) {
            assignCountry(elem.val());
          });

          scope.$watch(attrs.ngModel, function (country) {
            elem.val(country);
          });
        }
      }
    };
  }]);

Open in new window

0
Comment
Question by:EddieShipman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 500 total points
ID: 40231271
The template was made to use an array, but you're now trying to use an object. You need more than the call to join() to make a string containing the markup for <option> elements. If an additional change is need to answer the second part of your question, you'll need to find someone with experience using AngularJS.
    // shortening countries list
    var countries = {"AD":"Andorra", "AE":"United Arab Emirates", "AF":"Afghanistan", "AG":"Antigua and Barbuda", "AI":"Anguilla", 
                     "AL":"Albania", "AM":"Armenia", "AN":"Netherlands Antilles", "AO":"Angola", "AQ":"Antarctica", "AR":"Argentina", 
                     "AS":"American Samoa", "AT":"Austria", "AU":"Australia", "AW":"Aruba", "AX":"Åland Islands", "AZ":"Azerbaijan"};
    var countriesAr = [];
    angular.forEach(countries, function(value, key) {
      this.push('<option value="'+key+'"> + value + '</option>');
    },countriesAr);

    return {
      restrict: 'E',
      template: '<select>' + countriesAr.join('\n') + '</select>',
      replace: true,
      link: function (scope, elem, attrs) {
        // How do I modify these to use the code and not the country name?
        if (!!attrs.ngModel) {
          var assignCountry = $parse(attrs.ngModel).assign;

          elem.bind('change', function (e) {
            assignCountry(elem.val());
          });

          scope.$watch(attrs.ngModel, function (country) {
            elem.val(country);
          });
        }
      }
    };
  }]);

Open in new window

0
 
LVL 26

Author Comment

by:EddieShipman
ID: 40231661
Even adding that, I can't make it work, here's the fiddle:

http://jsfiddle.net/Mrbaseball34/GBW74/
0
 
LVL 58

Expert Comment

by:Gary
ID: 40312121
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question