使用 AngularJS 在第一个选择标签的基础上填充第二个选择标签



我正在使用UI-Bootstrap,所以我可以同时使用Angular和bootstrap。我想从"控制器国家"控制器具有的选择标签中访问该值,并将该值用作填充选择"选定服务"的参数。我想这样做是因为程序的逻辑是,当我在选择标签中选择国家/地区列表中的国家/地区时,使用该值填充另一个选择标签。

我的 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>

我的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");
          }
     });
});

现在,我可以访问第一个"控制器国家/地区"的值并将该值记录在控制台上,仅此而已。而且,正如你所看到的,我用第一个对象填充选择,但我希望它们是dinamic。谁能帮我。如果您发现我的代码是错误的,欢迎您修复它。

问候和感谢。

网页视图

首先,你不需要那个$watch,当选择值(ng-model(发生变化时,你可以使用ng-change来触发回调。 你可以在那里写你的if语句。

其次,不要在选择上使用ng-controller,您必须ng-options将嵌套的<option>元素动态放置在<select>内。

最后,如果要使用从选择数字

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 = { ... };
      }
 };

您的选择可能如下所示

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

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

最新更新