Solved

Dropdown selection using Angularjs

Posted on 2014-02-22
1
2,016 Views
Last Modified: 2014-02-24
Hi Experts,

I am using HTML5 and AngularJS and have a dropdown that contains state codes (Ex: AL,NY,CA,DE,FL,NJ etc) .For instance  if a user from New york state logon to the system then in the  dropdown "NY" should be selected , I will get current user state code via Angular service. Once I get current user from service in the controller I should bind it to dropdown .How can I achieve this using angularJS ? Any sample working code would be highly appreciated.

Thanks in Advance
0
Comment
Question by:ksd123
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39879216
Test page : http://jsfiddle.net/s2Ff9/

<div ng-app="myApp" ng-controller="myCtrl">
	<select ng-model="selectedState" ng-options="s.name for s in states"></select>
</div>

Open in new window


	var myApp = angular.module("myApp", []);

	var myCtrl = myApp.controller("myCtrl", ["$scope", "State", function($scope, State) {

		$scope.states = [];

		State.getStates().then(function(states) {
			$scope.states = states;
		});

		State.getMyState().then(function(state) {
			for(var s=0;s<$scope.states.length;s++) {
				if(state == $scope.states[s].abbreviation) {
					$scope.selectedState = $scope.states[s];
					break;
				}
			}
		});

	}]);

    myApp.factory("State", ["$q", "$timeout", function($q, $timeout) {
		return {
			getMyState : function() {
				var deferred = $q.defer();
				$timeout(function() { // WE USE A TIMER TO DO THE SIMULATION
					var s = "NY";
					deferred.resolve(s);
				}, 2000);
				return deferred.promise;
			},
			getStates: function() {
				var deferred = $q.defer();
				$timeout(function() { // WE USE A TIMER TO DO THE SIMULATION
					var data = [{"name":"Alabama","abbreviation":"AL"},{"name":"Alaska","abbreviation":"AK"},{"name":"American Samoa","abbreviation":"AS"},{"name":"Arizona","abbreviation":"AZ"},{"name":"Arkansas","abbreviation":"AR"},{"name":"California","abbreviation":"CA"},{"name":"Colorado","abbreviation":"CO"},{"name":"Connecticut","abbreviation":"CT"},{"name":"Delaware","abbreviation":"DE"},{"name":"District Of Columbia","abbreviation":"DC"},{"name":"Federated States Of Micronesia","abbreviation":"FM"},{"name":"Florida","abbreviation":"FL"},{"name":"Georgia","abbreviation":"GA"},{"name":"Guam","abbreviation":"GU"},{"name":"Hawaii","abbreviation":"HI"},{"name":"Idaho","abbreviation":"ID"},{"name":"Illinois","abbreviation":"IL"},{"name":"Indiana","abbreviation":"IN"},{"name":"Iowa","abbreviation":"IA"},{"name":"Kansas","abbreviation":"KS"},{"name":"Kentucky","abbreviation":"KY"},{"name":"Louisiana","abbreviation":"LA"},{"name":"Maine","abbreviation":"ME"},{"name":"Marshall Islands","abbreviation":"MH"},{"name":"Maryland","abbreviation":"MD"},{"name":"Massachusetts","abbreviation":"MA"},{"name":"Michigan","abbreviation":"MI"},{"name":"Minnesota","abbreviation":"MN"},{"name":"Mississippi","abbreviation":"MS"},{"name":"Missouri","abbreviation":"MO"},{"name":"Montana","abbreviation":"MT"},{"name":"Nebraska","abbreviation":"NE"},{"name":"Nevada","abbreviation":"NV"},{"name":"New Hampshire","abbreviation":"NH"},{"name":"New Jersey","abbreviation":"NJ"},{"name":"New Mexico","abbreviation":"NM"},{"name":"New York","abbreviation":"NY"},{"name":"North Carolina","abbreviation":"NC"},{"name":"North Dakota","abbreviation":"ND"},{"name":"Northern Mariana Islands","abbreviation":"MP"},{"name":"Ohio","abbreviation":"OH"},{"name":"Oklahoma","abbreviation":"OK"},{"name":"Oregon","abbreviation":"OR"},{"name":"Palau","abbreviation":"PW"},{"name":"Pennsylvania","abbreviation":"PA"},{"name":"Puerto Rico","abbreviation":"PR"},{"name":"Rhode Island","abbreviation":"RI"},{"name":"South Carolina","abbreviation":"SC"},{"name":"South Dakota","abbreviation":"SD"},{"name":"Tennessee","abbreviation":"TN"},{"name":"Texas","abbreviation":"TX"},{"name":"Utah","abbreviation":"UT"},{"name":"Vermont","abbreviation":"VT"},{"name":"Virgin Islands","abbreviation":"VI"},{"name":"Virginia","abbreviation":"VA"},{"name":"Washington","abbreviation":"WA"},{"name":"West Virginia","abbreviation":"WV"},{"name":"Wisconsin","abbreviation":"WI"},{"name":"Wyoming","abbreviation":"WY"}];
					deferred.resolve(data);
				}, 500);
				return deferred.promise;
			}
		}

	}]);

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
chrome and edge version of getElementById("p").attributes("title").value 2 36
key press alert 2 32
multiple selects 23 48
Only allow one check box 17 25
Find out what you should include to make the best professional email signature for your organization.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

839 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