1

I was wondering with AngularJS ng-options - is it possible to use 2 different variables for the name in the dropdown?

take this simplified example - I would like the dropdown to contain Country - City in the dropdown. I know this is simplistic but is this possible to do with combining the variables outside of the select dropdown?

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
</head>

<body ng-app="myapp">
<div ng-controller="HelloController" >

<select ng-model="blah" ng-options="city.Id as city.CityName for city in cities"></select>
</div>
<script>
angular.module("myapp", [])
    .controller("HelloController", function($scope) {
        $scope.cities = [
   {Id: '000001', Country:'USA', CityName: 'Chicago'},
   {Id: '000002', Country:'Ireland', CityName: 'Dublin'},
   {Id: '000003', Country:'Spain', CityName: 'Barcelona'},
   {Id: '000004', Country:'USA', CityName: 'Boston'},
];

    } );
</script>
</body>
</html>

1 Answer 1

2

You should be able to accomplish that like so:

ng-options="city.Id as (city.Country + ' - '  + city.CityName) for city in cities"
Sign up to request clarification or add additional context in comments.

1 Comment

thats was it - sorry for the noob question. Thanks John

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.