如果在 select 框中找不到匹配项,请重置过滤器

Reset the filter if match not found in select box

我正在尝试创建一个过滤器,它将根据 select 框中的 selection 在 table 中显示结果。

此外,默认情况下 'City' select 框将设置用户位置,我正在抓取并设置 ng-init="my.city='${city}'"。因此,默认结果将根据用户位置进行过滤。

我遇到的唯一问题是,当用户位置与我的 'City' select 框中的任何值都不匹配时,应该将其重置。即 'Select a city' 选项应该是 selected,并且所有结果将在不应用任何过滤器的情况下显示。

在我的代码中,如果我 select 任何一个城市然后尝试 select 'Select a city' 过滤器没有被重置'。

我该如何解决这个问题?

完整代码如下:

var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', '$filter', function($scope, $filter) {
 $scope.loc = {
  Sydney: 4,
  Toronto: 7,
  London: 3,
  Berlin: 7
 };
 $scope.country = ['Australia', 'India', 'Germany', 'China', 'Canada', 'United Kingdom'];
 $scope.fullData = [{
  name: 'ABC',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'CDE',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'DEF',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'CDE',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'ABC',
  countryName: 'United Kingdom',
  city: 'London'
 }, {
  name: 'ABC',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'DEF',
  countryName: 'Canada',
  city: 'Toronto'
 }, {
  name: 'DEF',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'BCD',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  countryName: 'Canada',
  city: 'Toronto'
 }];
}]);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div class="container">
  <div ng-app="myApp" ng-controller="myCtrl">
    <div class="row">
      <div class="col-md-3">
        <label>Search Keyword:</label>
        <input type="text" class="form-control" ng-model="my.$"/>
      </div>
      <div class="col-md-3">
        <label>Country:</label>
        <select class="form-control" ng-model="my.countryName" ng-options="countryName as countryName for countryName in country">
          <option value="" selected>Select a country</option>
        </select>
      </div>
      <div class="col-md-3">
        <label>City</label>
        <select class="form-control" ng-model="my.city" ng-init="my.city='${city}'" ng-options="key as key for (key, value) in loc">
          <option value="" selected>Select a city</option>
        </select>
      </div>
    </div>
    <hr />
    <div class="row">
      <table class="table table-bordered">
        <tr>
          <th>Name</th>
          <th>Country</th>
          <th>City</th>
        </tr>
        <tr ng-repeat="item in fullData | filter: my">
          <td>{{item.name}}</td>
          <td>{{item.countryName}}</td>
          <td>{{item.city}}</td>
        </tr>
      </table>
    </div>
  </div>
</div>

在这种情况下,过滤器应用在空对象中,该对象是一个 属性 命名的城市,但是应用过滤器的所有 属性 对象的第一个值应该为空。

您必须在您的控制器中初始化您的对象

 $scope.my={$:'',countryName:'',city:''};

并删除 ng-init 城市 属性

尝试以下

var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', '$filter', function($scope, $filter) {
$scope.my={$:'',countryName:'',city:''};
 $scope.loc = {
  Sydney: 4,
  Toronto: 7,
  London: 3,
  Berlin: 7
 };
 $scope.country = ['Australia', 'India', 'Germany', 'China', 'Canada', 'United Kingdom'];
 $scope.fullData = [{
  name: 'ABC',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'CDE',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'DEF',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'CDE',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'BCD',
  countryName: 'Australia',
  city: 'Sydney'
 }, {
  name: 'ABC',
  countryName: 'United Kingdom',
  city: 'London'
 }, {
  name: 'ABC',
  countryName: 'China',
  city: 'Shanghai'
 }, {
  name: 'DEF',
  countryName: 'Canada',
  city: 'Toronto'
 }, {
  name: 'DEF',
  countryName: 'India',
  city: 'Bangalore'
 }, {
  name: 'BCD',
  countryName: 'Germany',
  city: 'Berlin'
 }, {
  name: 'ABC',
  countryName: 'Canada',
  city: 'Toronto'
 }];
}]);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div class="container">
  <div ng-app="myApp" ng-controller="myCtrl">
    <div class="row">
      <div class="col-md-3">
        <label>Search Keyword:</label>
        <input type="text" class="form-control" ng-model="my.$"/>
      </div>
      <div class="col-md-3">
        <label>Country:</label>
        <select class="form-control" ng-model="my.countryName" ng-options="countryName as countryName for countryName in country">
          <option value="" selected>Select a country</option>
        </select>
      </div>
      <div class="col-md-3">
        <label>City</label>
        <select class="form-control" ng-model="my.city"  ng-options="key as key for (key, value) in loc">
          <option value="" selected>Select a city</option>
        </select>
      </div>
    </div>
    <hr />
    <div class="row">
      <table class="table table-bordered">
        <tr>
          <th>Name</th>
          <th>Country</th>
          <th>City</th>
        </tr>
        <tr ng-repeat="item in fullData | filter: my">
          <td>{{item.name}}</td>
          <td>{{item.countryName}}</td>
          <td>{{item.city}}</td>
        </tr>
      </table>
    </div>
  </div>
</div>