Angularjs angular js搜索栏,仅返回我在搜索栏中输入的数据

Angularjs angular js搜索栏,仅返回我在搜索栏中输入的数据,angularjs,Angularjs,我试图在搜索栏中输入国家/地区,请使用ng click或ng submit仅返回我在搜索栏中输入的国家/地区的数据。现在,当我提交时,我会返回每个国家,然后我可以过滤以获得我想要的国家。我的所有数据都在mongolab中,我使用node js将数据发送到angular js服务,然后我有一个angular js控制器将数据发送到我想要的视图。我正在使用anguler用户界面路由器。这是我的服务: angular.module('TravelSite').service('countryS

我试图在搜索栏中输入国家/地区,请使用ng click或ng submit仅返回我在搜索栏中输入的国家/地区的数据。现在,当我提交时,我会返回每个国家,然后我可以过滤以获得我想要的国家。我的所有数据都在mongolab中,我使用node js将数据发送到angular js服务,然后我有一个angular js控制器将数据发送到我想要的视图。我正在使用anguler用户界面路由器。这是我的服务:

    angular.module('TravelSite').service('countryService', ['$http', '$q', function($http, $q) {

this.getCountry = function() {
  var dfd = $q.defer();
    $http({
      method: 'GET',
      url: '/country'
    }).then(function(response) {
      console.log(response.data);
      dfd.resolve(response.data);
      }, function(err) {
    console.log('Error: ' + err);
  });
    return dfd.promise;
  };

this.addCountry = function(body) {
var dfd = $q.defer();
  $http({
    method: 'POST',
    url: '/country',
    data: body
  }).then(function(response){
    console.log(response.data);
    dfd.resolve(response.data);
  }, function(error) {
    console.log('error: ' + error);
  });
    return dfd.promise;

  };

}]);
这是我的控制器:

    angular.module('TravelSite').controller('CtrlCountry', function($scope, countryService) {



$scope.addCountry = function(country, cb) {

    countryService.addCountry(country).then(function(res) {

    });
}

$scope.getCountry = function() {

    countryService.getCountry().then(function(dataFromService) {
        $scope.countries = dataFromService;

    });
}
}))

以下是我的看法:

       <div>

    <div>
        <input type="text" ng-model="search.name">      

        <input type="text" placeholder="name" ng-model="country.name"/> 
        <input type="text" placeholder="population" ng-model="country.population"/>
        <input type="text" placeholder="travel warning" ng-model="country.travel_warning"/>  
        <input type="text" placeholder="visa" ng-model="country.visa"/>
        <input type="text" placeholder="vaccinations" ng-model="country.vaccinations"/>

        <button ng-click="addCountry(country)">Create Country</button> 
    </div>

    <form ng-submit="getCountry()">

        <input type="text" ng-model="country.name"> 

        <input type="submit" value="Search">
    </form>




        <button ng-click="addCountry(country)">Create Country</button> 
    <ul>
         <li ng-repeat="country in countries | filter:search">{{country.name}}
         {{country.population}}  {{country.travel_warning}}  {{country.visa}}  {{country.vaccinations}}</li>
    </ul>



</div>

创建国家
创建国家
  • {{country.name} {{country.population}{{country.travel{{country.visa}{{{country.vasculations}}{
我只编写了大约一个月的代码,如果有人能帮助我,我将非常感激

谢谢,对于这个看起来很糟糕的代码,我先表示歉意。

我也是新来的。:)

我有一些代码,完全一样。我得到很多信息,然后只过滤掉我想要的

要进行筛选,您所能做的只是对视图进行一些更改。 在搜索输入框中,您可以执行以下操作:

<input type='text' ng-model='search'>

然后,在重复ng时,您可以使用:

<li ng-repeat="country in countries | filter: {name: search}">
  • 如果要筛选所有字段,可以使用以下选项:

    <li ng-repeat='country in countries | search'>
    

  • 希望能有所帮助

    谢谢!,是的,它很有帮助,它正在做我现在想要它做的事情。如果它有帮助,并且你的问题得到解决,你能标记这个答案吗?:)