foreach loop in angularjs

foreach loop in angularjs


Today we discussed foreach loop in angularjs  web services more then data format gives and this data different array format change used angularjs data foreach how to array get the data follow us. Angularjs set Cookies

foreach loop in angularjs
foreach loop in angularjs


Foreach syntax:
angular.forEach($scope.data, function(value, key){});

Json File:
[
   {
     "Name": "Dev2tricks",
     "Password": "developerinvention"
   },
   {
     "Name": "developerinvention",
     "Password": "Dev2tricks"
   }
]

Services.js
var app = angular.module('testModule', []);
app.controller('testController', function($scope, $http){
   $http.get('Data/info.json').then(
      function(data){
         $scope.data = data;
      }
   );
   angular.forEach($scope.data, function(value, key){
      if(value.Password == "thomasTheKing")
         console.log("username is thomas");
   });
});
I know the $scope.init = function() is causing the problem, if you check the link above everything should become clear.

Could anybody explain exactly what this code is doing?
function(genre, index) - what does the index mean?

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

// Create and drop in as a service factory
barsApp.factory('Venues', function(){
    // This will return the Venues object
    var Venues = {};
    // Array of objects
    Venues.list = [
        { id: 0,
            name: 'Bar One',
            genres: ['Rock','Metal','Dubstep','Electro']
        },
        { id: 1,
            name: 'Bar Two',
            genres: ['Indie','Drumstep','Dubstep','Electro']
        },
        { id: 2,
            name: 'Bar Three',
            genres: ['Rock','Metal','Thrash Metal','Heavy Metal']
        },
        { id: 3,
            name: 'Bar Four',
            genres: ['Pop','RnB','Hip Hop']
        }
    ];
    return Venues;
});
// Setup controller, provide venues model into our scope
function DevCtrl($scope, Venues){
    $scope.venue = Venues;
    $scope.availableGenres = [];
    $scope.genreFilter = null;
    
    $scope.init = function() {
        angular.forEach(Venues.list, function(genre, index){
            //Only add to the availableGenres array if it doesn't already exist
            var exists = false;
            angular.forEach($scope.availableGenres, function(avGenre, index){
                if (avGenre == genre) {
                    exists = true;
                }
            });
            if (exists === false) {
                $scope.availableGenres.push(genre);
            }
        });
    
    };

$scope.setGenreFilter = function(genre) {
        $scope.genreFilter = genre;
    };
}

View.html
<div ng-app="barsApp" ng-controller="DevCtrl" ng-init="init()">
    <ul>
        <li ng-repeat="venue in venue.list | filter:genreFilter">
        <a href="">{{venue.name}}</a></li>
        <li>total {{venue.list.length}}</li>
    </ul>
        <label>
foreach loop in angularjs Dev2Tricks 5 of 5
foreach loop in angularjs Today we discussed foreach loop in angularjs   web services more then data format gives and this data differ...

Share this

Related Posts

Previous
Next Post »