Search code examples
javascriptangularjsng-tags-input

AngularJS 1.x NgTagsInput show message


I have this code using AngularJS and NGTagsInput. I'm using Filter in AutoComplete, and you can add new itens pressing 'Enter', but i wanted show this message to user. If there is not a result in auto complete, show message: "No results found. Press Enter to Add" I tried put a Else inside Filter. but does not work because he checks every single letter.

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

Here is a Plnkr: PLUNKR

Thanks for now ! =)


Solution

  • You just need to check if there are a returned item matched, in other way just check if the array filtered with that query has an item. If there are no matched item this mean no data :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