【问题标题】:AngularJS 1.x NgTagsInput show messageAngularJS 1.x NgTagsInput 显示消息
【发布时间】:2017-07-03 14:56:33
【问题描述】:

我有这段代码使用 AngularJS 和 NGTagsInput。 我在自动完成中使用过滤器,您可以按“Enter”添加新项目,但我想向用户显示此消息。如果自动完成没有结果,显示消息:“未找到结果。按 Enter 键添加” 我尝试在过滤器中放置一个 Else。但不起作用,因为他检查了每一个字母。

      $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

现在谢谢! =)

【问题讨论】:

    标签: javascript angularjs ng-tags-input


    【解决方案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

    【讨论】:

    • 非常感谢!奇迹般有效! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2017-01-19
    • 1970-01-01
    • 2019-10-11
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多