Solved

AngularJS Country select directive

Posted on 2014-07-30
4
1,498 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
4 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now