Angularjs: loop through the intersection value of an array with another array in a view

I am struggling with the following. I have a global array1 with values ​​of FileA, FileB, FileC, FileD and FileE. Then I have a specific array2 with the values ​​FileA and FileC.

The result that I would like to get is similar to

<div class="matched">FileA</div>
<div class="not_matched">FileB</div>
<div class="matched">FileC</div>
<div class="not_matched">FileD</div>
<div class="not_matched">FileE</div>

I was thinking in a ng snooze with a custom filter, but I can't figure out how to do this.

Here is an attempt that doesn't even compile

HTML

<body ng-app="myModule">
<div ng-controller="myController">
    <div ng-repeat="entity in entities">
        <div ng-repeat="myEntity in myEntities |  lookInside(entity)">
            {{myEntity.match}} - {{myEntity.name}}
        </div>
     </div>
</div>
</body>

and js

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

myModule.controller('myController', ['$scope',  function($scope) {
    $scope.entities = ['fileA', 'fileB', 'fileC', 'fileD', 'fileE'];
    $scope.myEntities = ['fileA', 'fileC'];
}]);

myModule.filter('lookInside', function(){
    return function(items, name){
        var arrayToReturn = [];
        var name = {};
        for (var i=0; i<items.length; i++){
            name.match = 'no';
            name.name = items[i];
            if (items[i] == name) {
                name.match = 'si';
            }
            arrayToReturn.push(name);
        }
        return arrayToReturn;
    };
});

http://jsfiddle.net/C5gJr/46/

What is the best approach to follow here?

Greetings

UPDATE: I decided only with a filter for each record that checks if it is inside the array

<body ng-app="myModule">
<div ng-controller="myController">
    <div ng-repeat="entity in entities">
       {{entity | lookInside: myEntities}}
    </div>
</div>
</body>

and js

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

myModule.controller('myController', ['$scope',  function($scope) {
    $scope.entities = ['fileA', 'fileB', 'fileC', 'fileD', 'fileE'];
    $scope.myEntities = ['fileA', 'fileC'];
}]);

myModule.filter('lookInside', function(){
    return function(item, array){
        var name = 'no';
        for (var i=0; i<array.length; i++){
            if (array[i] == item) {
                name = 'si';
            }
        }
        return name;
    };
});

http://jsfiddle.net/C5gJr/48/

, ( ). , .

+4
2

, ng- scope .

http://jsfiddle.net/VrB3H/

<div ng-repeat="entity in entities" ng-class="{'matched': isMatch(entity), 'not_matched': !isMatch(entity)}">
        {{isMatch(entity)}} - {{entity}}
 </div>

myModule.controller('myController', ['$scope',  function($scope) {
    $scope.entities = ['fileA', 'fileB', 'fileC', 'fileD', 'fileE'];
    $scope.myEntities = ['fileA', 'fileC'];

    $scope.isMatch = function(entity)
    {
        return $scope.myEntities.indexOf(entity) >= 0;
    }
}]);
+3

, easy_to_understand (IMO), ( ).

0

Source: https://habr.com/ru/post/1527139/


All Articles