Solved

AngularJS Country select directive

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

930 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

13 Experts available now in Live!

Get 1:1 Help Now