2017-08-14 39 views
0

一個數組所以我有兩個數組,一個看起來有點liek這AngularJs,搜索對另一

$scope.users = {id: 1, email: xxx} {id: 2, email: xxx}.... 

和我有這個一個

$scope.booking = {id: 20, regid: 2} .... 

所以用戶被顯示在一個表在這一刻。如果用戶有預訂,我希望錶行有一個紅旗反對它(不能共享表,但它是一個基本的ng-repeat)因此,如果usersid在預訂數組中存在regid,推動一些東西用戶

任何幫助將是邪惡

+0

請閱讀這篇文章對如何問StackOverflow的一個問題:https://stackoverflow.com/help/how-to-ask的 –

+0

可能的複製[如何在JavaScript中比較數組?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) –

回答

0

首先你所有的$scope.users$scope.booking的對象不是數組...

我做出了榜樣如何知道用戶是否有書,所以繼承人是代碼和 plnkr

魔法在foreach和for循環中。

HTML

<!DOCTYPE html> 
<html ng-app="plunker"> 

    <head> 
    <meta charset="utf-8" /> 
    <title>AngularJS Plunker</title> 
    <script>document.write('<base href="' + document.location + '" />');</script> 
    <link rel="stylesheet" href="style.css" /> 
    <script data-require="[email protected]" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script> 
    <script src="app.js"></script> 
    </head> 

    <body ng-controller="MainCtrl"> 

    <table> 
     <thead> 
     <th>ID</th> 
     <th>NAME</th> 
     <th>HAS BOOK</th> 
     </thead> 
     <tbody> 
     <tr ng-repeat="user in users"> 
      <td>{{user.id}}</td> 
      <td>{{user.name}}</td> 
      <td>{{user.hasBook}}</td> 
     </tr> 
     </tbody> 
    </table> 
    </body> 

</html> 

控制器

var app = angular.module('plunker', []); 

app.controller('MainCtrl', function($scope) { 
    $scope.name = 'World'; 


    $scope.users = [ 
    {id:1, name:'john'}, 
    {id:2, name:'barney'}, 
    {id:3, name:'moroni'}, 
    {id:4, name:'adam'}, 
    {id:5, name:'sam'} 
    ]; 

    $scope.books = [ 
    {id:1, regId:1, name:'book1'}, 
    {id:2, regId:2, name:'book1'}, 
    {id:5, regId:3, name:'book1'}, 

    ]; 

    angular.forEach($scope.users, function(value, index){ 
    for(var i =0; i< $scope.books.length; i++){ 
     if(value.id === $scope.books[i].id){ 
     value.hasBook = true; 
     } 
    } 
    }); 
    console.log($scope.users); 


}); 
-1

您可以使用過濾器來查找是否有conrcrete用戶ID的任何用戶在預訂表。 檢查這個例子:

[http://jsbin.com/ewitun/1/edit?html,js,output][1]