过滤方法-Angular.JS

Filter method - Angular.JS

本文关键字:JS -Angular 方法 过滤      更新时间:2023-09-26

我想在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()???

要在控制器中使用过滤器,应该注入$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]);
});