2016-07-22 50 views
2

這裏是我的控制器:與選擇NG重複NG-變化下拉菜單隻觸發了一次

 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-變化觸發一次,在第一產品類型的變化。我也想在更改時傳遞選定的產品類型。有人可以幫助我如何做到這一點?

回答

2

我遇到了類似的問題。我能夠修復它的方式是使用ng-options指令,而不是查看選項。

這裏是角的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