简体   繁体   English

使用AngularJS在第一个Select标签上填充第二个Select标签?

[英]Fill second Select tag base on the first Select tag using AngularJS?

I'm using UI-Bootstrap so I can use them both Angular and bootstrap. 我正在使用UI-Bootstrap,因此可以同时使用Angular和bootstrap。 I want to access the value from a select tag that the "controllerCountries" controller have and use that value as a parameter to the fill the select "selectedServices". 我想从“ controllerCountries”控制器具有的选择标记中访问值,并将该值用作填充选择“ selectedServices”的参数。 I want to do that because the logic of the program will be that when I choose a country of a list of countries in a select tag, use that value to fill the another select tag. 我要这样做是因为程序的逻辑将是,当我在一个选择标记中选择一个国家列表中的一个国家时,请使用该值填充另一个选择标记。

My HTML is here: 我的HTML在这里:

<div  style="margin-top: 15px"  class="col-md-6">
     <div id="form-pais"class="form-group">
         <label class=" control-label">Country:</label>
         <div class="selectContainer">
              <select  ng-controller="controllerCountries" class="form-control" id="abvCountry" name="abvCountry" ng-model="countrySelected">
                   <option value="gt">Guatemala</option>
                   <option value="sl">El Salvador</option>
                    <option value="hn">Honduras</option>
               </select>
         </div>
     </div>
</div>
<div style="margin-top: 15px"  class="col-md-6">
   <div class="form-group">
       <label class=" control-label">Services:</label>
   <div ng-controller="controllerServices" class="selectContainer">
       <select  class="form-control" id="selectServices"ng-model="servicios" ng-options="y for (x, y) in serviciosgt" text="">
       </select>
   </div>

My JS file looks like that: 我的JS文件如下所示:

//Heres the list of services in the objects. I use this to fill the second select.
app.controller('controllerServices', function($scope)
{
     $scope.serviciosgt =
     {
          consMiami : "Consolidación Miami",
          contCompletosGT: "Contenedores Completos",
          cargMartConsolidadGT : "Carga Maritima Consolidada",
          cargAereaRegularGT: "Carga Áerea Regular"
    };

    $scope.serviciosSL =
    {
         contCompletosSl : "Contenedores Completos",
         cargMartConsolidadSl: "Carga Marítima Consolidada",
         cargAereaRegularSl: "Carga Áerea Regular"
    };

    $scope.serviciosHN =
    {
         contCompletosHN : "Contenedores Completos",
         cargMartConsolidadHN: "Carga Marítima Consolidada",
         cargAereaRegularHN: "Carga Áerea Regular"
    };
});

//Heres the controller to fill.
app.controller('controllerCountries', function($scope)
{
     $scope.countrySelected ='';
     $scope.$watch('countrySelected', function () {
          if($scope.countrySelected=="gt")
          {
               console.log("Select GT");

          }
          else if($scope.countrySelected=="sl")
          {
               console.log("Select SL");
          }
          else if($scope.countrySelected=="hn")
          {
               console.log("Select HN");
          }
     });
});

Right now I can access the value of the first "controllerCountries" and log that value on the console, but thats it. 现在,我可以访问第一个“ controllerCountries”的值并将其记录在控制台上,仅此而已。 And, as you can see, I fill the select with the first object, but I wan them to be dinamic. 而且,如您所见,我将第一个对象填充到选择对象中,但我希望它们具有动态效果。 Can anyone help me please. 谁能帮我。 And if you look that my code is wrong, youre welcome to fix it. 如果您发现我的代码有误,欢迎您修复它。

Greetings and thanks. 问候和感谢。

HTML view HTML视图

First of all, you don't need that $watch , you can use ng-change to fire a callback when the select value (ng-model) is changed. 首先,您不需要$watch ,可以ng-change选择值(ng-model)时使用ng-change触发回调。 you can write your if statements there. 您可以在其中编写if语句。

Second, don't use ng-controller on a select, you have ng-options to dynamically place nested <option> elements inside the <select> . 其次,不要在SELECT上使用ng-controller ,您可以使用ng-options将嵌套的<option>元素动态放置在<select>

Lastly, if you want to fill select number 2 using a value you've selected from select number 1, just reference the correct data when the ngChange callback is fired 最后,如果您要使用从选择数字1中选择的值来填充选择数字2,则只需在触发ngChange回调时引用正确的数据

$scope.onCountryChange = function (country) {
      if(country=="gt")
      {
           console.log("Select GT");
           $scope.serviciosgt = { ... };
      }
      else if(country=="sl")
      {
           console.log("Select SL");
           $scope.serviciosgt = { ... };
      }
      else if(country=="hn")
      {
           console.log("Select HN");
           $scope.serviciosgt = { ... };
      }
 };

your select could look something like this 您的选择可能看起来像这样

<select class="form-control" 
        name="abvCountry" 
        ng-change="onCountryChange(countrySelected)" 
        ng-options="country for country in countries" ng-model="countrySelected">
</select>

While

$scope.countries = ["gt", "sl", "hn"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM