如何选择下拉值并使用angularjs填充下拉列表?

时间:2016-05-05 10:14:56

标签: html angularjs angularjs-scope

如何设置值并使用angularjs填充下拉列表?

我已在两个下拉列表中成功设置了该选项。

在我的下拉列表中,一个依赖于另一个,即父母有相应的孩子

假设在我的数据库中我存储的父名是Sally,子名是SallyChild2,

在我的html页面中,当我点击编辑此内容时,我需要在我的下拉框中填充整个数据,并选择此特定父名称为Sally,子名称为SallyChild2

所以我需要将Child - SallyChild2更改为Child - SallyChild1

所以我硬编码为此但不能正常工作

$scope.selectedParent="Sally";
$scope.selectedChild="SallyChild2";

但它不起作用。

编辑内容将在drobdown菜单中选择的内容时唯一的事情..

这是我的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": "George",
      "childName": "George Child1"         
     },
     {
      "parentName": "Folly",
      "childName": "FollyChild1"        
     },
     {
      "parentName": "Sally",
      "childName": "Sally Child1"         
     },

     {
      "parentName": "George",
      "childName": "GeorgChild2"         
     },
     {
      "parentName": "Folly",
      "childName": "FollyChild2"        
     },
     {
      "parentName": "Folly",
      "childName": "FollyChild3"         
     },
     {
      "parentName": "Sally",
      "childName": "SallyChild2"         
     }
    ];

    $scope.selectedParent="Sally";
    $scope.selectedChild="SallyChild2";

    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>

1 个答案:

答案 0 :(得分:0)

这是级联下拉列表的示例示例:

html文件:      

    <script type="text/javascript"      src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script>
        function CountryCntrl($scope) {
            $scope.countries = {
                'India': {
                    'Maharashtra': ['Pune', 'Mumbai', 'Nagpur', 'Akola'],
                    'Madhya Pradesh': ['Indore', 'Bhopal', 'Jabalpur'],
                    'Rajasthan': ['Jaipur', 'Ajmer', 'Jodhpur']
                },
                'USA': {
                    'Alabama': ['Montgomery', 'Birmingham'],
                    'California': ['Sacramento', 'Fremont'],
                    'Illinois': ['Springfield', 'Chicago']
                },
                'Australia': {
                    'New South Wales': ['Sydney'],
                    'Victoria': ['Melbourne']
                }
            };
        }
    </script>
</head>
<body>

    <div ng-controller="CountryCntrl">
        <div>
            Country: 
            <select id="country" ng-model="states" ng-options="country for (country, states) in countries">
                <option value=''>Select</option>
            </select>
        </div>
        <div>
            States: <select id="state" ng-disabled="!states" ng-model="cities" ng-options="state for (state,city) in states"><option value=''>Select</option></select>
        </div>
        <div>
            City: <select id="city" ng-disabled="!cities || !states" ng-model="city"><option value=''>Select</option> <option ng-repeat="city in cities" value='{{city}}'>{{city}}</option></select>        
        </div>
    </div>

</body>