2017-10-18 53 views
0

我想從list1更新list2,並且我使用了ng-change。在選擇不工作ng-change

我的應用程序已完全配置,並且html頁面的控制器設置良好。

,當我從我的第一個列表1選擇elemt方法updateList2從未invocked

<label>NameLIst1</label> 
<select ng-model="idLIST1" 
     ng-change="updateList2(idLIST1)" 
     ng-options="elt.id as elt.name for elt in list1"> 

     <option value="">select one</option> 
</select> 

<label>Name LIST 2</label> 
<select ng-model="idLIST2"     
     ng-options="elt.id as id.name for elt in list2"> 

     <option value="">select one</option> 
</select> 


$scope.updateList2 = function(id){ 
alert("id ="+id); 
} 
+0

控制檯中的錯誤?該應用程序可以工作嗎? –

+0

請上傳一個[Plunker](https://plnkr.co)... –

+0

你不需要傳遞一個參數給'updateList2'函數。 '$ scope.updateList2 = function(){alert($ scope.idLIST1)}' – Zooly

回答

1

我有寫在下面的代碼,試試這個 它的做工精細,也PARAM工作的唯一的事, 但您不需要在方法中傳遞參數

<div class="container" ng-controller="TestController"> 
    <label>NameLIst1</label> 
    <select ng-model="idLIST1" 
      ng-change="updateList2(idLIST1)" 
      ng-options="elt.id as elt.name for elt in list1"> 

     <option value="">select one</option> 
    </select> 

    <label>Name LIST 2</label> 
    <select ng-model="idLIST2" 
      ng-options="elt.id as id.name for elt in list2"> 

     <option value="">select one</option> 
    </select> 
</div> 

var app = angular.module("test", ["ngRoute"]) 
     .controller("TestController", ['$scope', function ($scope) { 
      $scope.list1 = [ 
       { id: 1, name: 'data1' }, 
       { id: 2, name: 'data2' }, 
       { id: 3, name: 'data3' }, 
       { id: 4, name: 'data4' }, 
      ]; 
      $scope.updateList2 = function (id) { 
       alert("id =" + id); 
      } 
     } 
     ]);