跟踪select - AngularjS的价值

时间:2015-02-11 19:22:15

标签: angularjs

我要做的是有一个带有“标签”的下拉选项。选择标签后,我的ng-repeat中的项目将显示或隐藏,具体取决于其中一个标签是否与所选标签匹配。

我的问题是我似乎无法获得当前选择的标记值。

在我的控制器中我有

// Items that are shown or hidden if the selected tag, matches one of theirs.
$scope.items = [
    {
      source: "../assets/images/FirstItem.png",
      tags: ["All","App","Dat","Man"]
    },
    {
     source: "../assets/images/SecondItem.png",
     tags: ["All","App"]
    }
  ];

  // All the possible tags (what populates the select/dropdown)
  $scope.tags = [
    {
      name:"All",
      tag:"All",
    },
    {
      name:"Applications",
      tag:"App"
    },
    {
      name:"Data Center",
      tag:"Dat",
    }
  ];

  // Updates the currentTag, so I can filter the items.
  $scope.newTag = function($scope) {
    $scope.currentTag = $scope.selectedItem.tag;
  };

  // Is the currently selected tag
  $scope.currentTag = "All";

我的HTML

<select>
  <option ng-repeat="tag in tags" ng-change="newTag()" ng-model="currentTag.tag">{{tag.name}}</option>
</select>
<div ng-repeat="item in items">
  <div class="small-12 columns">
      <img src="{{item.source}}" ng-show="somehow filter by currentTag" />
  </div>
</div>

所以我的问题是跟踪当前所选标签的简单方法。或者从选择区域,tag.tag。我一直在尝试几个类似问题的例子,但似乎没有什么对我有用,我认为我遗漏了一些简单的东西,因为我还是AngularJS的新手。

我设置得很差,我总是乐于改进/正确编码!

1 个答案:

答案 0 :(得分:1)

您应该将ng-optionsselect元素结合使用:

<select ng-options="tag as tag.name for tag in tags" ng-model="currentTag.tag" ng-change="newTag()"></select>