Reputation: 143
So, I've been looking around to understand why this happens. From what I understand after going through couple of other questions on Stackoverflow and other websites is that, this problem is caused because ng-model
is being initialised to a value not present in the list of options.
But this is not an issue on the first drop-down in my code and I am not able to figure out what's going on wrong for the other 3 drop-downs.
Here is my fiddle - https://jsfiddle.net/7w4owwsk/6/
The markup:
<div class="container main-form" ng-app="searchApp">
<div class="row" ng-controller="SearchScope as scope">
<div class="col-md-2">
<ul class="nav nav-pills nav-stacked">
<li ng-repeat="item in scopes" ng-class="{'active': isScopeActive({{$index}})}"><a href="" ng-click="changeScope($index)">{{item}}</a></li>
</ul>
</div>
<div class="col-md-10">
<div id="form0" ng-show="isScopeActive(0)" ng-controller="SingleIPController as sip">
</div>
<div id="form1" ng-show="isScopeActive(1)" ng-controller="BulkIPController as bip">
<form class="form-horizontal panel panel-default form-panel" ng-show="searchEnabled">
<div class="form-group">
<label for="subnet" class="col-sm-2 control-label">Subnet</label>
<div class="col-sm-4">
<select class="form-control" id="subnet" ng-model="selectedSubnet" ng-options="x as y for (x, y) in trackedSubnets"></select>
</div>
</div>
<div class="form-group">
<label for="occtype" class="col-sm-2 control-label">Type</label>
<div class="col-sm-4">
<select class="form-control" id="occtype" ng-model="selectedType" ng-options="x as y for (x, y) in ipOccupancy"></select>
</div>
</div>
<div class="form-group">
<label for="team" class="col-sm-2 control-label">Team</label>
<div class="col-sm-4">
<select class="form-control" id="team" ng-model="selectedTeam" ng-options="x as y for (x, y) in teams"></select>
</div>
</div>
<div class="form-group">
<label for="machineType" class="col-sm-2 control-label">Machine Type</label>
<div class="col-sm-4">
<select class="form-control" id="machineType" ng-model="selectedMachineType" ng-options="x as y for (x, y) in machineType"></select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" ng-click="lookupData()">Search</button>
</div>
</div>
</form>
<div class="progress" ng-show="WIP">
<div class="progress-bar progress-bar-striped" ng-class="{'active': WIP}" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width: 80%">
<span class="sr-only">80% Complete</span> Working on it..
</div>
</div>
<div class="page-action" ng-hide="searchEnabled">
<button type="button" class="btn btn-default pull-left" ng-click="enableSearch()">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> Modify Search
</button>
<div class="col-sm-3 col-xs-8">
<input class="form-control" type="text" placeholder="Search" ng-model="searchText" />
</div>
</div>
<table ng-show="bulkIPData" class="table table-hover">
<thead>
<tr>
<th>I.P Address</th>
<th>Owner</th>
<th>Type</th>
<th>Team</th>
<th>Remarks</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in bulkIPData | filter:searchText">
<td>{{item.individualIP}}</td>
<td>{{item.owner}}</td>
<td>{{item.serverType}}</td>
<td>{{item.teamName}}</td>
<td>{{item.remarks}}</td>
<td><span class="glyphicon glyphicon-pencil edit-icon-btn" aria-hidden="true" ng-click="updateItem(item)"></span><a ng-href="mailto:{{item.emailId}}"><span class="glyphicon glyphicon-envelope edit-icon-btn" aria-hidden="true"></span></a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
JS:
(function() {
var app = angular.module("searchApp", []);
app.factory('SubnetFact', function() {
return {
"Select": "Select",
"10.78.90": "10.78.90",
"10.78.91": "10.78.91",
"10.78.92": "10.78.92",
"10.78.93": "10.78.93",
"10.78.94": "10.78.94",
"10.78.95": "10.78.95",
"10.107.45": "10.107.45"
};
});
app.factory('OccupFact', function() {
return {
"All IPK": "All IP",
"Free IPK": "Free IP",
"Used IPK": "Used IP"
};
});
app.factory('TeamFact', function() {
return {
"ALL": "All",
"Team A": "Team A",
"Team B": "Team B",
"Team C": "Team C",
"Team D": "Team D"
};
});
app.factory('MachineTypeFact', function() {
return {
"ALL": "ALL Servers and Devices",
"A": "A",
"B": "B",
"C": "C",
"D": "D"
};
});
app.controller("SearchScope", function($scope) {
$scope.activeScope = 1;
$scope.scopes = ['Individual IP Data', 'All IP Data'];
$scope.isScopeActive = function(num) {
return num === $scope.activeScope;
};
$scope.changeScope = function(index) {
$scope.activeScope = index;
};
});
app.controller("SingleIPController", function($rootScope, $scope, $http) {
});
app.controller("BulkIPController", function($rootScope, $scope, $http, SubnetFact, OccupFact, TeamFact, MachineTypeFact) {
$scope.trackedSubnets = SubnetFact;
$scope.ipOccupancy = OccupFact;
$scope.teams = TeamFact;
$scope.machineType = MachineTypeFact;
$scope.selectedSubnet = $scope.trackedSubnets["Select"];
$scope.selectedType = $scope.ipOccupancy["All IPK"];
$scope.selectedTeam = $scope.teams["ALL"];
$scope.selectedMachineType = $scope.machineType["ALL"];
$scope.bulkIPData = null;
$scope.WIP = false;
$scope.searchEnabled = true;
$scope.enableSearch = function() {
$scope.searchEnabled = true;
};
});
})();
The drop-down initialisation works as intended for the Subnet options and defaults to showing Select
as intended.
Can someone take a look and help with understanding the issue here?
Upvotes: 0
Views: 68
Reputation: 4414
You need to change the way you are setting default values.
Since you are using x as y in (x, y)
you should set the select ng-model to key
$scope.selectedSubnet = "Select";
$scope.selectedType = "All IPK";
$scope.selectedTeam = "ALL";
$scope.selectedMachineType = "ALL";
Or you can use the option suggested by @Lazar Yanchev
Updated fiddle
Upvotes: 0
Reputation: 101
Set this value to ng-options expression ng-options="y for (x, y) in ..."
.
With the notation x as y in (x, y)
you are using the key of the source object 'x' for binding to the model and the value of the object 'y' for visualizing the options. In the first case it works, because the name of the key and the value are equal.
Upvotes: 2