2013-10-29 67 views
1

我在我的項目中使用了Siyfion's typeahead directiveAngular typeahead:觀察數據集更改

我在控制器做的是這樣的:

$http({method: 'GET', 'url': CONFIG.SOMEURL})        
    .success(function(data, status, headers , config){      
    if(status==200)               
    {                   
     $scope.objects = data;             
     for(i=0; i < $scope.objects.length; i++){        
      $scope.allObjects.push($scope.objects[i].name); 
     }                  
     console.log($scope.allObjects);          

$scope.dataSet = {               
    name : 'objs',               
    local : $scope.allObjects                            
};     

我得到「對象」的數組作爲JSON從django視圖。但問題是在我的HTML模板:

<input type="text" class='sfTypeahead' datasets='dataSet' ng-model='testname' /> 

由於dataSet最初是空的,而異步調用正在取得我得到一個錯誤:

TypeError: Cannot read property 'name' of undefined

有沒有一種方法,如果我能請在dataSetdatasets中查找更改,以便在陣列allObjects獲得填充後,DOM可能會刷新?

任何人都可以提出什麼可以在這裏完成

+1

只需使用http://angular-ui.github.io/bootstrap/#/typeahead,而不是像這裏描述的那樣與promise一起工作nativelly:http://stackoverflow.com/a/15930592/1418796 –

+0

它會支持bs3 ?我目前正在使用bs3。 –

+1

是的,它應該與BS3沒有問題 –

回答

1

全面披露:我上述指令的作者。

基本上,您正在嘗試在Angular本身中執行所有預取操作,當時Bloodhound是專門爲您設計的。仔細查看https://twitter.github.io/typeahead.js/examples/並特別注意Bloodhound建議引擎。

也許有些用處:

注:你使用該指令的版本(10月前)沒有支持更新數據集局部變量,但現在確實如此,所以你可能這樣做,如果你喜歡。