简体   繁体   English

过滤方法-Angular.JS

[英]Filter method - Angular.JS

I would like to use method filter in Angular.JS and take first element with name Games 我想在Angular.JS中使用方法过滤器,并将第一个元素命名为Games

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

in HTML is very easy take this but in JS I don`t know how :( 在HTML中非常容易,但是在JS中我不知道如何:(

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

To use filters in your controller you should inject $filter . 要在控制器中使用过滤器,应注入$filter After that, you can get first element using [0] . 之后,您可以使用[0]获得第一个元素。

Example; 例;

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]);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM