javascript - 选择下拉菜单上的 ng-change 和 ng-repeat 仅触发一次

标签 javascript angularjs

这是我的 Controller :

     App.controller('LicenseController', ['$scope', 'licenseService', function($scope, licenseService) {
      var self = this;

      $scope.productTypes = {name:'', productID:''};
      self.productTypes = {name:'', productID:''};
      $scope.selectedProduct = {name:'', productID:''};
      $scope.productVersions = {productVersion:'', versionID:''};
      self.productVersions = {productVersion:'', versionID:''};

      $scope.fetchProductType = function(){
          licenseService.fetchProductType()
              .then(
                           function(d) {
                                self.productTypes = d;
                                $scope.productTypes = d;
                                console.log($scope.productTypes[1].name);
                                console.log($scope.productTypes[1].productID);
                           },
                            function(errResponse){
                                console.error('Error while fetching Currencies');
                            }
                   );
      };

      $scope.fetchProductType(); 

      $scope.getProductVersions = function(){

          console.log($scope.selectedProduct.productID);
          licenseService.getProductVersions($scope.selectedProduct.productID)
                  .then( 
                          function(d) {
                              $scope.productVersions = d;
                              self.productVersions = d;
                              console.log($scope.productVersions[1].productVersion);
                              console.log($scope.productVersions[1].versionID);
                         },
                          function(errResponse){
                               console.error('Error while creating User.');
                          } 
              );
      };

  }]);

和 HTML:

<tr>
                                                            <td>
                                                              <label for="sel1">Product Type</label>
                                                              <select class="form-control" id="sel1" ng-model=productTypes ng-change="getProductVersions()">
                                                                <option ng-repeat="type in ctrl.productTypes" value="" >
                                                                    {{type.name}} 
                                                                </option>                                                            
                                                              </select>
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <td>
                                                              <label for="sel1">Product Version</label>
                                                              <select class="form-control" id="ver" ng-model="productVersions" >
                                                                <option ng-repeat="version in ctrl.productVersions" value="" >
                                                                    {{version.productVersion}} 
                                                                </option>                                                            
                                                              </select>
                                                            </td>
                                                        </tr>

ng-change 仅在第一次更改产品类型时触发一次。我还想在更改时传递所选的产品类型。有人可以帮助我如何做到这一点吗?

最佳答案

我遇到了类似的问题。我能够修复它的方法是使用 ng-options 指令,而不是查看选项。

这是 Angular 1.x 文档中的示例:

    <select ng-model="myColor" ng-options="color.name group by color.shade for color in colors">

https://docs.angularjs.org/api/ng/directive/ngOptions

关于javascript - 选择下拉菜单上的 ng-change 和 ng-repeat 仅触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38533131/

相关文章:

php - 如何在 Angular JS中将selectBox中的数据填充为默认从mysql数据库中选择的当前项目

javascript - Restangular:错误:未知提供者:RestangularProvider <- Restangular

angularjs - Angular 指令中的 "terminal"选项是什么?

javascript - 如何在angularjs中使用ui-router获取url参数

javascript - 不要直接在触摸时显示悬停

javascript - JQuery - 更改绑定(bind)到 $elem.on ('click' ) 事件的函数

javascript - Bacon.js 中的 mergeWith 与 onValues

javascript - 你如何使用 FB.UI 自动发布?

javascript - 在 Internet Explorer 中使用 selenium 的 JavascriptExecutor 打开一个新选项卡

angularjs - angularjs 指令中的 iScroll