仅当值与JSON数组匹配时才显示元素

Jay*_*Jay 2 angularjs

我试图只有uid在JSON数组中存在时才显示元素:

到目前为止我已经尝试过了:

var app = agular.module('myApp', []);
app.controller('myCtrl', function () {
    $scope.uid = '10';
    $scope.datas = [{
        'id': '1',
        'name': 'abc'
    }, {
        'id': '2',
        'name': 'xyz'
    }];
});



<p ng-if="datas.length | filter:{id: uid}">ID exist in array</p>
Run Code Online (Sandbox Code Playgroud)

DEMO FIDDLE

mar*_*inn 5

你可以定义范围上的函数,用于u(并在内部使用过滤器服务)

<div ng-app="mod">
    <div ng-controller='MCtrl'>
       <p ng-show="exists(1)">ID exist in array</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)
mod.controller('MCtrl', function($scope, $filter){
    var items = [123,2,1]
    $scope.exists = function(val){
        return $filter('filter')(items, val).length > 0;;;
    }  
});
Run Code Online (Sandbox Code Playgroud)

如果你想在视野中这样做

<p ng-show="(items|filter:1:true).length > 0">ID exist in array</p>
Run Code Online (Sandbox Code Playgroud)

顺便说一句,真的意思是它应该完全匹配