如何使用angularJS填充下拉列表?
我已在两个下拉列表中成功设置了该选项。
在我的下拉列表中,一个依赖于另一个,即父母有相应的孩子
假设在我的数据库中,我存储的父名是London,子名是London State2,
在我的html页面中,当我点击编辑此内容时,我需要在我的下拉框中填充整个数据,选择此特定父名称为“London”,子名称为“London State2”
编辑内容将在drobdown菜单中选择的内容时唯一的事情..
即。我想在打开此页面时默认选择父名称作为伦敦,子名称是伦敦州名称
当我打开此页面时,我的问题是默认为父名称是--Select--而子名称是--Select--
这是我的script.js
的script.js
var app=angular.module('TestApp', ['angular.filter','ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('category',
{
views : {
"body" :
{
url : '/category',
templateUrl : 'category.html',
controller : 'TestController'
}
}
})
.state('category.subcategory',
{
url : '/subcategory',
views : {
"subbody@category" :
{
templateUrl : 'sample.html',
controller : 'SampleController'
}
}
})
});
app.controller('MainController', MainController);
function MainController($scope,$state)
{
alert("This is MainController")
$scope.getCategory=function()
{
$state.go('category');
}
}
app.controller('TestController', TestController);
//TestController.$inject['dataservice'];
function TestController($scope, $state){
$scope.data=[
{
"parentName": "India",
"childName": "India State1"
},
{
"parentName": "USA",
"childName": "USA State1"
},
{
"parentName": "London",
"childName": "London State1"
},
{
"parentName": "India",
"childName": "India State2"
},
{
"parentName": "USA",
"childName": "USA State2"
},
{
"parentName": "USA",
"childName": "USA State3"
},
{
"parentName": "London",
"childName": "London State2"
}
];
$scope.selectedParent="London";
$scope.selectedChild="London State2";
function onParentChange(parent){
if(!parent){
$scope.child = undefined;
}
}
$scope.getValue=function()
{
alert("Call to Sample Controller")
var currentState = $state.current.name;
var targetState = 'category.subcategory';
if(currentState === targetState)
$state.go($state.current, {}, {reload: true});
else
$state.go(targetState);
$state.go(".subcategory");
//$state.go('category.subcategory');
}
}
app.controller('SampleController', SampleController);
function SampleController($scope,$state)
{
alert("This is SampleController")
}
的index.html
<!DOCTYPE html>
<html ng-app="TestApp">
<head>
<link rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-route.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.js"></script>
<script src="angular-ui-router.js"></script>
<script src="script.js"></script>
<script src="angular-filter.js"></script>
</head>
<body ng-controller="MainController">
<a href="#" ng-click="getCategory()">Click to Category</a>
<div ui-view="body"></div>
</body>
</html>
category.html
<div>
<hr>
<div class="col-md-3">
<form>
<div class="form-group">
<label for="exampleSelect1"><b>Parent</b></label>
<select ng-model="selectedParent"
ng-init="selectedParent = null"
ng-change="onParentChange(selectedParent)"
class="form-control" id="data">
<option value="">--Select--</option>
<option ng-repeat="(key,value) in data | orderBy:'parentName'| groupBy:'parentName'">{{key}}</option>
</select>
</div>
</form>
</div>
<div class="col-md-3">
<form>
<div class="form-group">
<label for="exampleSelect1"><b>Child Names</b></label>
<select class="form-control" id="childnames" ng-model="child"
ng-disabled="!selectedParent"
ng-options="data.childName for data in data| filter: {parentName:selectedParent} | removeWith:{childName : 'Infant Food'}" ng-change="getValue()">
<option value="">--Select--</option>
</select>
</div>
</form>
</div>
<div ui-view="subbody"></div>
</div>
sample.html
<div>
Sample Controller
</div>
答案 0 :(得分:0)
从您的示例中,您希望伦敦应该是您下拉列表中的默认值..您可以通过这样的方式实现此目的而不是ng-init,
$scope.selectedParent = 'London'
$scope.selectedParent = $scope.data[2]['parentName'] // it will show london according to your eg data file.
//for child,
$scope.child = 'London State1'
$scope.child = $scope.data[2]['childName']
当您从前端的下拉列表中选择值时,这将自动更新。