2017-07-03 62 views
0

我有这个代码使用AngularJS和NGTagsInput。 我在AutoComplete中使用过滤器,并且可以按'Enter'添加新的itens,但是我想将此消息显示给用户。如果没有自动完成的结果,则显示消息:“未找到结果,按Enter键添加” 我试过在滤波器内部放置一个Else。但不起作用,因为他检查每一个字母。AngularJS 1.x NgTagsInput显示消息

 $scope.loadCountries = function($query) { 
    return $http.get('countries.json', { cache: true}).then(function(response) { 
     var countries = response.data; 
     return countries.filter(function(country) { 
     return country.name.toLowerCase().indexOf($query.toLowerCase()) != -1; 
     }); 
    }); 
    }; 
}); 

这里是一个Plnkr:PLUNKR

感谢吧! =)

回答

1

你只需要检查是否有一个返回的项目匹配,换句话说,只是检查用该查询过滤的数组是否有一个项目。如果没有匹配的项目这意味着没有数据:d

$scope.loadCountries = function($query) { 
    return $http.get('countries.json', { cache: true}).then(function(response) { 
     var countries = response.data; 
     var filtered = countries.filter(function(country) { 
     return country.name.toLowerCase().indexOf($query.toLowerCase()) != -1; 
     }); 
     $scope.nodata = filtered.length === 0; 
     return filtered; 
    }); 
    }; 

http://plnkr.co/edit/fo1lExzjz0eJxloaljd0?p=preview

+0

非常感谢!奇迹般有效! :d – Johnson