我想在 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/