2013-10-29 68 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月前)没有支持更新数据集局部变量,但现在确实如此,所以你可能这样做,如果你喜欢。