1

I am using Siyfion's typeahead directive in my project.

What I do in my controller is something like this :

$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                                                                                                             
};                

I get an array of 'objects' as json from django view. But the problem is in my HTML template:

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

Since the dataSet initially is empty while the async call is being made I get an error :

TypeError: Cannot read property 'name' of undefined

Is there a way if I can look for changes in dataSet or datasets so that as soon as the array allObjects gets populated,The DOM gets refreshed maybe ?

Can anyone suggest what could be done here

Deepankar Bajpeyi
  • 5,661
  • 11
  • 44
  • 64

1 Answers1

1

Full disclosure: I'm the author of the aforementioned directive.

Essentially, you are trying to do all the prefetching in Angular itself, when Typeahead.js & Bloodhound are designed specifically to do this for you. Take a good look at https://twitter.github.io/typeahead.js/examples/ and pay particular attention to the Bloodhound suggestion engine.

Perhaps of some use:

NOTE: The version of the directive you were using (9 months ago) didn't support updating the dataset local variable, but it now does, so you could do it that way, if you liked.

Siyfion
  • 979
  • 1
  • 12
  • 32