How to use lodash or any alternative to angular.foreach in angular2

150 views
Skip to first unread message

Rohit luthra

unread,
May 17, 2016, 8:30:51 AM5/17/16
to AngularJS
How to use lodash or any alternative to angular.foreach 

var data = {
    TimeStamp : "2016-04-25 12:50:00"
    };
    $http({
       method: 'POST',
       url: $location.protocol()+ '://' + $location.host() +  ':' + $location.port() + '/serverutilizationreport',
       headers: {'Content-Type': 'application/x-www-form-urlencoded'},
       data: $httpParamSerializerJQLike(data),
    }).success(function(data) {
    $scope.pieData = [];
    $scope.chartData = [];
                    $scope.requesttypes = [];
                    $scope.uniqrequesttypes = [];
                    $scope.transactionResponseTimeArray = [];
                    $scope.transactionCountArray = [];
                    $scope.totalRendererFailedTransactions;
                    $scope.totalRendererCompletions;
                    //$scope.requestcount = [];
                    angular.forEach(data, function(record) {
                    var rendererOutTime = record.RendererOutTime;
            var rendererInTime = record.RendererInTime;
                    var requestURI = record.RequestURI;
                    var timeTakenMS = record.TimeTakenMS;
                    var errorMessage = record.ErrorMessage;
                    if(errorMessage && rendererOutTime && rendererInTime){
                    $scope.totalRendererFailedTransactions++;
        }

        if(rendererOutTime && rendererInTime){
        $scope.totalRendererCompletions++;
        }
       
            if(requestURI && rendererOutTime && rendererInTime){
            $scope.transactionResponseTimeArray.push(timeTakenMS);
            $scope.transactionCountArray.push($scope.transactionResponseTimeArray.length)
            nameSpace = requestURI.substring(1);
            if(nameSpace.indexOf('/') != -1){
            nameSpace= nameSpace.substring(0, nameSpace.indexOf('/'));
            }
            else if(nameSpace.indexOf('?') != -1){
            nameSpace= nameSpace.substring(0, nameSpace.indexOf('?'));
            }
            else{
            nameSpace= nameSpace.substring(0, nameSpace.length);
            }
            if(nameSpace){
            if($scope.uniqrequesttypes.indexOf(nameSpace) == -1){
            $scope.uniqrequesttypes.push(nameSpace);
            }
            $scope.requesttypes.push(nameSpace);
            }
            }
                    });
Message has been deleted

Carlos Esteban Lopez Jaramillo

unread,
May 18, 2016, 9:13:43 AM5/18/16
to AngularJS
You sure you mean Ng2? this code looks like Ng1 to me, Ng2 deleted the use of the '$scope' variable, anyway the use is not that complex, just import the lodash function you need and replace it where you need it:
angular.forEach(data, (record) => {...})
// If using the global way
_
.each(data, (record) => {...})
// If using the specific import
each
(data, (record) => {...})

The importing is up to your build process, if Ng1 most likely you just add a script tag up in the html before your script tags, in Ng2 it can be done the same, or you can import just that specific function.
Reply all
Reply to author
Forward
0 new messages