AngularJS:如何在ng-click时更新选择选项值?

时间:2015-03-24 07:05:14

标签: javascript angularjs angularjs-ng-repeat angularjs-ng-click angularjs-ng-model

我有一个工作选择选项,其中几个值过滤数据用于搜索。

现在我需要一些链接作为补充工具栏,这有助于直接点击和更改以上选择选项

请参阅我制作的链接Website Link


登录示例:

  

用户:recobee@gmail.com

     

传递:kishore

http://proconstruct.co.in/jsearch


Javascript:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  var json = {
    "modules":
        <?php echo $qres; ?>
  };

  $scope.ocw = json;

  var allCategories = json.modules.map(function(item) {
    return item.designation
  });
  var filteredCategories = [];

  var allCities = json.modules.map(function(item) {
    return item.city
  });
  var filteredCities = [];


  allCategories.forEach(function(item) {
    if (filteredCategories.indexOf(item) < 0 && item) {
      filteredCategories.push(item);
    }
  });

  allCities.forEach(function(item) {
    if (filteredCities.indexOf(item) < 0 && item) {
      filteredCities.push(item);
    }
  });

  $scope.search = {
    designation: ""
  }
  $scope.search = {
    city: ""
  }

  $scope.categories = filteredCategories;

  $scope.updateFilter = function(value) {
    $scope.search.designation = value;
  }


  $scope.cities = filteredCities;
  $scope.updateFilter = function(value) {
    $scope.search.city = value;
  }

});

这些是链接html:

<div  ng-repeat="designation in categories ">
    <a ng-click="updateFilter(search.designation)"> {{designation}}</a>
</div>

这是正在运作的选择,必须通过链接做出反应:

<select ng-model="search.designation">
    <option value="">All Categories</option>
    <option ng-repeat="category in categories" value="{{category}}">{{category}}</option>
</select>

1 个答案:

答案 0 :(得分:2)

通过快速查看代码,我会尝试更改以下内容:

<div  ng-repeat="designation in categories ">
     <a ng-click="updateFilter(designation)"> {{designation}}</a>
</div>

我唯一改变的是updateFilter函数的参数。

[更新]

您还有两个具有相同名称的函数:updateFilter

试试这个:

$scope.search = {
    designation: "",
    city: ""
}

$scope.updateFilterDesignation = function(value) {
    $scope.search.designation = value;
}
$scope.updateFilterCity = function(value) {
    $scope.search.city = value;
}

所以链接应该这样做:ng-click =&#34; updateFilterDesignation(指定)&#34;

这应该有希望。