2016-05-04 29 views
0

我想在我的Bootstrap輸入中使用Typeahead。我在$ scope.regions中獲取信息和存儲。這裏是我的代碼:Angular - 在輸入中使用typeahead

myfile.js:

  $scope.selectedRegion = ""; 
      $scope.regions = res.data.result; 

,這裏是我的元素的截圖:

enter image description here

我想用regionName在輸入鍵盤緩衝。這裏是我的輸入HTML代碼:

<input placeholder="select region" class="form-control form-xs typeahead" ng-model="selectedRegion" typeahead="region for region in regions($viewValue)"> 

我要檢查regionName列表如下:

  <li ng-repeat="region in regions"> 
       <a href = "#"> {{ region.regionName }} </a> 
      </li> 
      </ul> 

好吧,我可以看到列表的結果。但Typeahead不起作用,我的意思是沒有出現輸入框中的typeahead。任何建議?

+0

用戶輸入在每次在myfile.js中都被清除$ http.get req uest。刪除$ scope.selectedRegion =「」;聲明。 – tejesh95

+0

我刪除它,但它仍然不起作用 – AFN

+0

更清楚地解釋什麼是不工作? – tejesh95

回答

0

使用下面提到的typeahead。因爲您需要單獨獲取regionName的值,並且無法分析鍵入的對象。

<input placeholder="select region" class="form-control form-xs typeahead" ng-model="selectedRegion" typeahead="region.regionName for region in regions($viewValue)"> 

或者添加onselect函數來測試預先輸入的值。

0

您可以使用過濾器

<input placeholder="select region" class="form-control form-xs typeahead" ng-model="selectedRegion" typeahead="region for region in regions | filter:$viewValue"> 
0

首先在你的控制器(例如autocompleteRegions())誰過濾數據並返回它們作爲一個簡單的數組創建一個函數:

// test if string start with prefix 
    function stringStartsWith (string, prefix) { 
     var minl = (prefix.length < string.length) ? prefix.length : string.length ; 
     return string.slice(0, minl) == prefix.slice(0, minl); 
    } 
    $scope.autocompleteRegions = function(value){ 
     var returnMe = []; 
     // find with regionName begin with "value" 
     for(var i in $scope.regions){ 
      if(stringStartsWith($scope.regions[i]["regionName"],value){ 
       returnMe.push( $scope.regions[i]["regionName"]) ; 
      } 
     } 
     return returnMe ; 
    } 

然後調用這個函數輸入類型頭

 <input typeahead="item for item in autocompleteRegions($viewValue)"> 
相關問題