1

I would like to use method filter in Angular.JS and take first element with name 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 :(

$scope.example.filter(model:Games).TakeFirst()???
2
  • 3
    Create custom filter which will return return arr.find(o => o.model === name); Commented Aug 2, 2016 at 13:55
  • @Tushar Yes it`s working correct - give this as answer :) Commented Aug 2, 2016 at 14:02

1 Answer 1

2

To use filters in your controller you should inject $filter. After that, you can get first element using [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]);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.