用uib-typeahead过滤AngularJs是行不通的

时间:2017-04-15 23:45:22

标签: angularjs filter angular-ui-bootstrap

我有一个使用角度uib-typeahead的应用程序。

通过ajax调用远程加载数据。

我需要过滤结果,只显示结果" name"包含$ viewValue string。

这是我的代码。我的问题是数据永远不会被过滤。

我做错了什么?

//标记

<input type="text" ng-model="modelo.tuss" placeholder="Select TUSS" 
    uib-typeahead="item as item.name for item 
    in getTabelaTUSS($viewValue) | filter:{name:$viewValue}" 
class="form-control">

//控制器

angular.module("clinang").controller('exameCtrl',['$scope', function($scope) {
      var prof=[{"id":1,"name":"John Prof"},
      {"id":2,"name":"Mary Prof"}];

      $scope.getTabelaTUSS = function(val) {
        return dataService.getTabelaTUSS().then(function(response){
             return prof; //only to simulate results to test
    });
  };

}]);

更新了控制器:

//first option - geting rid off view filter and making local filter in controller
angular.module("clinang").controller('configAgendaAddProcedimentosCtrl',['$scope','dataService','$state','$filter',function($scope,dataService,$state,filter){
      $scope.getTabelaTUSS = function(val) {
        return dataService.getTabelaTUSS().then(function(response){
                 return filterFilter(response.data, val); 
        });
      };
}]);

 //second option - using view filter and no local filter in controller
 //I also tried without success
angular.module("clinang").controller('configAgendaAddProcedimentosCtrl',['$scope','dataService','$state','$filter',function($scope,dataService,$state,filter){
      $scope.getTabelaTUSS = function(val) {
        return dataService.getTabelaTUSS().then(function(response){
                 return response.data; 
        });
      };
}]);

2 个答案:

答案 0 :(得分:0)

如果您将以下块替换为return prof;,它将按照我在this Plunker

中演示的预期工作
return dataService.getTabelaTUSS().then(function(response){
  return prof; //only to simulate results to test
});

return语句的问题是它返回一个由dataService.getTabelaTUSS().then()调用返回的promise对象。要解决此问题,请将prof分配给范围变量,并在视图中使用它,而不是在then中返回prof

答案 1 :(得分:0)

您正在调用Before Conversion: R: 128 G :39 B :50 H: 352.58426 S: 69.53125 V :100.0 After conversion R: 158 G: 126 B: 233 方法,但您从未在控制器中注入dataService.getTabelaTUSS()

按如下方式在控制器中注入dataService

dataService

注意:不要忘记在HTML代码中加入angular.module("clinang").controller('exameCtrl',['$scope','dataService' function($scope,dataService) { var prof=[{"id":1,"name":"John Prof"}, {"id":2,"name":"Mary Prof"}]; $scope.getTabelaTUSS = function(val) { return dataService.getTabelaTUSS().then(function(response){ return prof; //only to simulate results to test }); }; }]); 服务文件。

另外,如果我们看到您未使用dataService方法dataService.getTabelaTUSS()的代码,则返回response对象。因此,如果您不需要服务中的数据,请按以下步骤更新您的方法。

prof
相关问题