对象的角度过滤器

时间:2016-11-07 18:40:36

标签: angularjs object filter

我正在尝试使用过滤器,以便使用名称为ID的ng-options匹配select元素中新选择的名称。

我正在使用的对象如下:

 $scope.fish = {1:'Ben', 2: 'Rich', 3:'John'}

现在,默认情况下,select设置为“John”。当用户将选择选项更改为“Rich”时,我希望过滤器功能返回2(这是Rich的相应ID)。

假设:

$scope.selectedFish

仅存储鱼的名称而不是ID。

JSFiddle

2 个答案:

答案 0 :(得分:1)

尝试使用以下解决方案

Mat padding = Mat::zeros(dataMat.rows, dataMat.cols, datumMat.type());

希望这是有帮助的。

答案 1 :(得分:1)

你可以这样做,

<div data-ng-app="market">
  <div data-ng-controller="SomeController">
    <select ng-model="selectedFish" ng-options="key as value for (key , value) in fish" ng-change="getIdByName()"></select>
  </div>
</div>

<强>控制器:

app.controller('SomeController', ['$scope', '$filter', function($scope, $filter) {
  $scope.fish = {
    1: 'Ben',
    2: 'Rich',
    3: 'John'
  }
  $scope.getIdByName = function() {
    angular.forEach($scope.fish, function(value, key) {
      if (key == $scope.selectedFish) {
        console.log("selected key is " + $scope.selectedFish + "selected value is " + value);
      }
    });
  }

}]);

<强> DEMO