0

我正在使用UI-Bootstrap,所以我可以使用它们Angular和Bootstrap。我想访问“controllerCountries”控制器所具有的选择标签的值,并将该值用作参数来填充选择“selectedServices”。我想这样做是因为程序的逻辑是,当我在选择标记中选择一个国家列表的国家时,使用该值填充另一个选择标记。填写第二个使用AngularJS选择标签的第一个选择标签?

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

现在我可以访问的第一个 “controllerCountries” 的价值和日志在控制台上的价值,但多数民众赞成它。而且,正如你所看到的,我用第一个对象填充选区,但是我想让他们变成动态的。任何人都可以帮助我。如果你看我的代码是错误的,欢迎你解决它。

问候和感谢。所有的

HTML view

+0

[动态NG-选项(https://stackoverflow.com/que stions/26518220/angularjs动态-NG选项对链接两个下拉菜单) –

回答

0

首先,你不需要那么$watch,您可以使用ng-change火回调时选择值(NG-模型)被改变。你可以在那里写你的if报表。

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

最后,如果你想选择补数2使用你从选择1号选择的数值,只是参考当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"]; 
相关问题