简体   繁体   中英

filter array values in angularjs

I have an arrays of values that contain the title names of different applications.

Example:

$scope.badge = [{title: 'Incidents'},{title: 'News'}]

In the main page, where I place all the different apps, I would like to get the name for the specific app.

How can I filter the values so I can get the correct title for each app?

Something like this:

<span>{{badge.title}}</span>

UPDATE/ SOLUTION

I got it to work. The solution is below:

$scope.badges = {           
                     titles: [
                                 {id: 2, name: 'Incidents'},
                                 {id: 3, name: 'News'}
                             ]
                 };

<div ng-repeat="title in badges.titles | filter:{id:2}">    
    <ul>
        <li>                                                                
            <a class="{{title.name}}" ng-click="addNew()">Add New {{title.name}}</a>
        </li>
    </ul>

For my current purposes, that did it for me. If there is a more elegant, dynamic solution, I would like to learn it.

Many thanks to all.

If the browsers you're using support Array.prototype.find , you can do something like this.

$scope.badge = [{label: 'Incidents', appName: 'incident'},{label: 'News', appName: 'news'}];

//I'm not positive how to get the app name, but APP_NAME can be replaced with that
<span>{{badge.find(function(option) { return option.appName == APP_NAME})}}</span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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