javascript - 过滤方法 - Angular.JS

标签 javascript angularjs

我想在 Angular.JS 中使用方法过滤器,并使用名称为 Games 的第一个元素

$scope.example = [
        {model : "Games", id : "1"},
        {model : "Pictures", id : "2"},
        {model : "Cars", id : "3"},
        {model : "Games", id : "4"},
    ];

在 HTML 中很容易接受这个但在 JS 中我不知道如何:(

$scope.example.filter(model:Games).TakeFirst()???

最佳答案

要在 Controller 中使用过滤器,您应该注入(inject) $filter。之后,您可以使用 [0] 获取第一个元素。

例子;

var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', function($scope,$filter) {
    $scope.example = [
        {model : "Games", id : "1"},
        {model : "Pictures", id : "2"},
        {model : "Cars", id : "3"},
        {model : "Games", id : "4"},
    ];
    console.log( $filter('filter')($scope.example, { model: "Games" })[0]);
});

关于javascript - 过滤方法 - Angular.JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38722462/

相关文章:

javascript - 如何在 Javascript 中将 cd-words-wrapper 类设置为 535px 而不是 435px

javascript - else 条件不适用于 jquery 中的 src 属性

AngularJS ngResource 删除事件

javascript - Jquery 事件处理问题

javascript - 是否可以在具有真实(真实世界)尺寸的 html Canvas 上绘制?

html - ng-show/ng-hide 在 safari 中导致元素宽度为 0,高度为 0

javascript - rails 4 : Angular and JS only loading after page refresh

angularjs - 如何将cordova添加到现有的 Angular 项目

angularjs - 如何在具有隔离范围的指令中监听站点范围的事件

javascript - jPlayer-用海报播放一系列mp3文件