Solved

AngularJS Country select directive

Posted on 2014-07-30
4
1,599 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

791 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