2016-10-21 2 views
0

저는 AngularJS의 초보자입니다. 데이터베이스에서 오는 도시를 선택하는 데 사용되는 HTML 코드의 입력 텍스트 상자가 있습니다. 이를 위해 API를 호출합니다. 결과는 정상적으로 작동하는 데이터리스트를 채 웁니다. 그러나 데이터리스트에서 아이템을 선택하면 메소드를 다시 호출하고 내가 선택한 동일한 결과를 보여줍니다. 나는 그것이 변화 한 것이란 것을 압니다. 누구든지 나를이 문제에 대한 해결책이나 저를 도울 수있는 아이디어를 줄 수 있습니까? 사전에 도움을 주셔서 감사합니다데이터리스트에서 항목을 선택하면 변경 결과가 다시 표시됩니다.

내 HTML :

<input type="text" list="cityList" class="form-control" placeholder="Select City" ng-model="selectcity" ng-change="searcity()" class="form-control" id="seacityincoucat"> 
<datalist id="cityList"> 
    <option ng-repeat="city in cities.results" value="{{city.name}}"> 
</datalist> 

컨트롤러 :

$scope.searcity = function() { 
     var ciseurl = urlcs + $scope.selectcity; 
     $http.get(ciseurl, config).then(function (response) { 
      if (response.data.status === '$200') { 
       $scope.cities = response.data; 
       var x = $scope.cities.results; 
       couponSvc.setCityId(x[length].id); 
      }else{ 
       alert("try some thing else"); 
      } 
     }); 
    }; 
+0

ng-change 때문에 일어난다면 texbox의 값을 변경 했으므로 다시 호출 할 것입니다. ng-keyup을 시도하십시오. – Jigar7521

답변

0

사용 NG-흐림 대신 NG 변화

<input type="text" list="cityList" class="form-control" placeholder="Select City" ng-model="selectcity" ng-blur="searcity()" class="form-control" id="seacityincoucat"> 
+0

ng-blur를 사용했지만 입력 상자에 임의의 문자를 입력 할 때 그것은 데이터리스트에있는 모든 도시 목록을 가져 오기 위해 API를 호출해야하지만 이전처럼 작동하지 않습니다. –

+0

ok then user/ngKeyup – Sajeetharan

+0

ng-keyup에서 첫 번째 요소의 ID (즉, couponSvc.setCityId (x [길이] .id);) 데이터 목록에서 –

0

사용 ngKeyPress

<input type="text" list="cityList" class="form-control" placeholder="Select City" ng-model="selectcity" ng-keypress="searcity()" class="form-control" id="seacityincoucat"> 
+0

첫 번째 요소 ID를 설정합니다. 그래서 내가 변경을 사용합니다 –

+0

Plnkr을 만들 수 있습니까? – sisyphus

관련 문제