角度类型提前:观察数据集的变化

Angular typeahead : watch for dataset change

本文关键字:观察 数据集 变化 类型      更新时间:2023-09-26

我在我的项目中使用了Siyfion的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 可能会刷新?

谁能建议在这里可以做什么

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

从本质上讲,您正在尝试在Angular本身中进行所有预取,而Typeahead.js和Bloodhound是专门为您设计的。仔细看看 https://twitter.github.io/typeahead.js/examples/,并特别注意寻血猎犬建议引擎。

也许有一些用途:

  • https://twitter.github.io/typeahead.js/examples/#remote
  • https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#remote
  • https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#prefetch

注意:您使用的指令版本(9 个月前)不支持更新数据集局部变量,但现在支持,因此如果您愿意,可以这样做。