【问题标题】:Dynamic Drop Down AngularJS and Database with JSON使用 JSON 动态下拉 AngularJS 和数据库
【发布时间】:2015-08-29 06:01:24
【问题描述】:

我已经想出了如何使用 Angular 从数据库中填充第一个下拉菜单,但我被困在如何获取第一个下拉框的选定值以查询以填充第二个框。另外,如果数据在单独的表中,每次选择下拉菜单时我都必须进行 API 调用吗?我已经看到很多使用预设表执行此操作的示例,但没有使用实际的 api 调用。这就是我感到困惑的地方。

这是我现在拥有的。

app.controller('SelectOptGroupController', function($scope,$http) {
        $http.get('api2.php').
        success(function(data) {
            $scope.animals = data;
        });
         $scope.species= [];
         $scope.getOptions2 = function(){
            //NOT SURE FROM HERE
          };

  })

HTML 文件

<div ng-controller="SelectOptGroupController" class="md-padding selectdemoOptionGroups">
          <div>
            <h1 class="md-title">Select Animal</h1>
            <div layout="row">
              <md-select ng-model="animals" placeholder="Animals" ng-change="getOptions2()">
                <md-option ng-repeat="animal in animals" value="{{animal.animal}}">{{animal.animal}}</md-option>
              </md-select>
              <md-select ng-model="species" placeholder="Species">
                <md-option ng-repeat="spec in species" value="{{spec.animal_species}}">{{spec.animal_species}}</md-option>
              </md-select>

          </div>
        </div>

到目前为止,数据库有一个表,其中两列分别是动物和动物物种。同一种动物有多个物种,例如有三个动物名称为熊的插入物,但每个 animal_species 是不同的 grizzly、black 和 polar。感谢您的帮助!

【问题讨论】:

    标签: mysql json angularjs drop-down-menu


    【解决方案1】:

    对于您的第一个问题,md-select 的 ng-model 将对应于集合中选定的动物,因此在此示例中 $scope.model.selectedAnimal 将是您可以用来访问选定动物的变量。

    <div ng-controller="SelectOptGroupController" class="md-padding selectdemoOptionGroups">
        <div>
            <h1 class="md-title">Select Animal</h1>
            <div layout="row">
                <md-select ng-model="model.selectedAnimal" placeholder="Animals" ng-change="getSpecies()">
                    <md-option ng-repeat="animal in model.animals" value="{{ animal }}">{{ animal }}</md-option>
                </md-select>
                <md-select ng-model="model.selectedSpecies" placeholder="Species">
                    <md-option ng-repeat="species in model.species" value="{{ species.animal_species }}">{{ species.animal_species }}</md-option>
                </md-select>
            </div>
        </div>
    </div>
    

    至于您重复的 api 调用问题,您可以让数据库返回所有物种并管理 js 中的“活动”物种,或者使用服务来缓存每个请求,以便至少每个动物只会查询 api一次。这也将数据逻辑排除在控制器之外,使其可重用(以及在多个控制器/指令/服务之间轻松共享数据)。

    app.controller('SelectOptGroupController', ['$scope', 'animalService', function($scope, animalService) {
        $scope.model = {
            animals: [],
            species: [],
            selectedAnimal: '',
            selectedSpecies: {}
        };
    
        animalService.getAnimals().then(function(data){
            $scope.model.animals = data;
        });
    
        $scope.getSpecies = function(){
            animalService.getSpecies($scope.model.selectedAnimal).then(function(data){
                $scope.model.species = data;
            }); 
        };
    }]); //TYPO WAS HERE
    
    app.factory('animalService', ['$http', '$q', function($http, $q){
        var cache = {};
    
        return {
            getAnimals: function(){
                return $http.get('api2.php').then(function(result) {
                    return result.data;
                });
            },
            getSpecies: function(animal){
                if(cache[animal]){
                    return $q.when(cache[animal]);
                } else{
                    return $http({
                        url: 'whatever.php',
                        method: 'GET',
                        params: {
                            animal: animal
                        }
                    }).then(function(result){
                        cache[animal] = result.data;
                        return cache[animal];
                    });
                }
            }
        };
    }]);
    

    如果您想一次性获取所有物种并仅在前端进行过滤,您可以执行以下操作:

    app.controller('SelectOptGroupController', ['$scope', 'animalService', function($scope, animalService) {
        $scope.model = {
            animals: [],
            species: [], //this will contain selected animals species
            allSpecies: [], //this will contain all species for all animals
            selectedAnimal: '',
            selectedSpecies: {}
        };
    
        animalService.getAnimals().then(function(data){
            $scope.model.animals = data;
        });
    
        animalService.getSpecies().then(function(data){
            $scope.model.allSpecies = data;
        });
    
        $scope.getSpecies = function(){
            //this filters out the species for the selected animal
            $scope.model.species = $scope.model.allSpecies.filter(function(species){
                return species.animal === $scope.model.selectedAnimal;
            });
        };
    }]);
    
    app.factory('animalService', ['$http', '$q', function($http, $q){
        return {
            getAnimals: function(){
                return $http.get('api2.php').then(function(result) {
                    return result.data;
                });
            },
            getSpecies: function(animal){
                return $http.get('api3.php').then(function(result){
                    return result.data;
                });
            }
        };
    }]);
    

    【讨论】:

    • 谢谢!这正是我所需要的。
    • 我需要为此添加到 angular.module 的依赖项吗?尝试运行此程序时出现 $injector 错误。谢谢
    • 不,这些都是默认的角度服务,除了这里创建的animalService。我确实注意到控制器末尾有一个错字,我忘记添加右方括号 ( ] )。我更新了代码并添加了注释以指出我的错误所在。如果这不起作用,请告诉我注射器说找不到什么服务
    • 您的 api3.php 每次都返回每个物种。我的原始代码假设每个 api 调用都会返回特定的动物物种(如鹿)。如果您想继续一次返回所有内容,我添加了第二组代码
    • 是的,你会使用 where 语句:w3schools.com/sql/sql_where.asp,类似于 SELECT * FROM species WHERE animal = 'deer'(除了从 angular 传入的 php 变量)。我真的不知道php,所以我对那部分无能为力!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 2019-06-04
    • 2020-09-23
    • 2012-11-12
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多