Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use angular.foreach with ng-repeat?

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 786
    Comment on it

    We use ng-repeat to iterate over the properties of an object. It instantiates or clones a template once for each item in a collection.

    Similarly,angular.foreach invokes a function that is an iterator function once for each item in an obj collection.

    Here is an example of how you can use ng-repeat with angular.foreach to iterate over a collection to perform a task or a particular function.

    <li ng-repeat="historyKey in userstatsData">
                    <a href="javascript:;" class="callHistory">
                        <span class="datenew left" style="width:45%;">{{historyKey.created_at}}</span>
                        <span class="blank left" style="width:30%;">{{historyKey.displayname}}</span>
                        <span class="valuechange valuechangeNEW right" style="width:25%;"><b>${{historyKey.charge_rate}}</b></span>
                        <span class="clr"></span>
                    </a>
                </li>
    

    Now add this code into your js:

    libraryData['responsetype'] = 'json';
        libraryData['page'] = 1;
        console.log( JSON.stringify( libraryData ) );
        var responsePromise = $http.post( BASE_URL+"mylibrary", JSON.stringify( libraryData ) );
        responsePromise.success( function( data, status, headers, config ) {
                $scope.serverData = libraryData.data;
                console.log(JSON.stringify($scope.serverData));
                $scope.userstatsData = $scope.serverData.data.messages.msglogs;
                angular.forEach($scope.userstatsData , function(historyKey){
                        if(historyKey.displayname == username ){
                            historyKey.displayname = "me";
                        }
                    });
    });
    

    In the above code snippet I have sent a request to the server. All I wanted is to change the value of displayname to "me" everywhere in the column where it appears if it is same as the name of the username.

    You can use this as an example or guidance code if you want to use angular.foreach with ng-repeat.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: