I am working with a select element that has a name and id as options value. I am also adding '-' along with the name. However, there is a scenario where the name will be empty and in that case, the '-' should not be displayed.
The expected output should look something like this:
- name - 123
- 123
This is what I have attempted:
HTML:
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script>document.write("<base href=\"" + document.location + "\" />");</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<select ng-model="model.selected" ng-options="item.Title as item.Title + '-' + item.ID for item in items"></select>
<p>Selected: {{model.selected}}</p>
</body>
</html>
Controller:
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.model = {
selected : 'Chicago'
}
$scope.items = [
{ID: '000.000.0001', Title: 'Chicago'},
{ID: '000.000.0002', Title: 'New York'},
{ID: '000.000.0003', Title: 'Washington'}
];
});