json - Declare function inside another function in angularjs -
i have first function:
$scope.loaddatafromtomonth= function (from,to,year) { // $scope.loaddatafromtomontharrivee(from,to,2016); var url = servername+'admin/dashboard/getincidentdepartbymonthfromto/'+from+'/'+to+'/'+year; // alert(url); function onsuccess(response) { console.log("+++++getincidentdepartbymonthfromto success++++++"); if (response.data.success != false) { $scope.payloadgetincidentdepartbymonthfromto = response.data.data; var getincidentdepartbymonthfromto= $scope.payloadgetincidentdepartbymonthfromto; console.log(json.stringify(getincidentdepartbymonthfromto)); $scope.data = {}; // new object $scope.data.datasets = []; // new array in data object .. $scope.data.labels =[]; var thewholeob={}; var datasetobj = {}; //temp object push dataset array.. var datasetobjtwo = {}; /////////////anomalies depart datasetobj.data = []; datasetobj.label= 'my first dataset'; datasetobj.fillcolor='rgba(220,220,220,0.2)'; datasetobj.strokecolor= 'rgba(220,220,220,1)'; datasetobj.pointcolor= 'rgba(220,220,220,1)'; datasetobj.pointstrokecolor= '#fff'; datasetobj.pointhighlightfill= '#fff'; datasetobj.pointhighlightstroke='rgba(220,220,220,1)'; getincidentdepartbymonthfromto.foreach(function(data) { var monthnumber = $filter('date')(data.la_date, "mm"); var mun = data.number; $scope.data.labels.push(monthnumber); datasetobj.data.push(mun); }); $scope.data.datasets.push(datasetobj); } else { alert("failure"); } }; function onerror(response) { console.log("-------getincidentdepartbymonthfromto failed-------"); //$scope.stopspin('spinner-0'); console.log(response.data); console.log("inside getincidentdepartbymonthfromto error condition..."); }; //----make ajax request call data---- ajaxservicess.getdata(url,username,password, 'get', '').then(onsuccess, onerror); };
this function return result:
$scope.data = { labels: ['jan', 'feb' 'jul'], datasets: [ { label: 'my first dataset', fillcolor: 'rgba(220,220,220,0.2)', strokecolor: 'rgba(220,220,220,1)', pointcolor: 'rgba(220,220,220,1)', pointstrokecolor: '#fff', pointhighlightfill: '#fff', pointhighlightstroke: 'rgba(220,220,220,1)', data: [75, 59, 80, 81, 56, 55] } ] };
it works good.
i have second function returns different data:
$scope.loaddatafromtomontharrivee= function (from,to,year) { var url =servername+'admin/dashboard/getincidentarriveebymonthfromto/'+from+'/'+to+'/'+year; //alert(url); function onsuccess(response) { console.log("+++++getincidentarriveebydate success++++++"); if (response.data.success != false) { $scope.payloaddaymonthyeardata = response.data.data; var loadeddatabydaymonthyear= $scope.payloaddaymonthyeardata; alert('xxx'+json.stringify(loadeddatabydaymonthyear)); $scope.data = {}; // new object $scope.data.datasets = []; // new array in data object .. $scope.data.labels =[]; var thewholeob={}; var datasetobj = {}; //temp object push dataset array.. var datasetobjtwo = {}; /////////////anomalies arrivee datasetobjtwo.data = []; $scope.date=[]; datasetobjtwo.label='my second dataset'; datasetobjtwo.fillcolor= 'rgba(151,187,205,0.2)'; datasetobjtwo.strokecolor= 'rgba(151,187,205,1)'; datasetobjtwo.pointcolor= 'rgba(151,187,205,1)'; datasetobjtwo.pointstrokecolor= '#fff'; datasetobjtwo.pointhighlightfill='#fff'; datasetobjtwo.pointhighlightstroke= 'rgba(151,187,205,1)'; loadeddatabydaymonthyear.foreach(function(data) { var monthnumber = $filter('date')(data.la_date, "mm"); $scope.date.push(monthnumber); var mun = data.number; $scope.data.labels.push($scope.monthnumber); datasetobjtwo.data.push(mun); }); $scope.data.datasets.push(datasetobjtwo); } else { alert("failure"); } // $scope.stopspin('spinner-0'); }; function onerror(response) { console.log("-------getincidentarriveebydate failed-------"); //$scope.stopspin('spinner-0'); console.log(response.data); console.log("inside getincidentarriveebydate error condition..."); }; //----make ajax request call data---- ajaxservicess.getdata(url,username,password, 'get', '').then(onsuccess, onerror); };
this function return result:
$scope.data = { labels: [ 'jan', 'feb' 'jul','jul', 'aug', 'sep', 'oct', 'nov', 'dec'], datasets: [ { label: 'my second dataset', fillcolor: 'rgba(151,187,205,0.2)', strokecolor: 'rgba(151,187,205,1)', pointcolor: 'rgba(151,187,205,1)', pointstrokecolor: '#fff', pointhighlightfill: '#fff', pointhighlightstroke: 'rgba(151,187,205,1)', data: [ 102, 123, 145, 60, 161] } ] };
it works also, question is: how can declare second function inside first 1 , combine data returned , final result this:
$scope.data = { labels: [ 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'], datasets: [ { label: 'my second dataset', fillcolor: 'rgba(151,187,205,0.2)', strokecolor: 'rgba(151,187,205,1)', pointcolor: 'rgba(151,187,205,1)', pointstrokecolor: '#fff', pointhighlightfill: '#fff', pointhighlightstroke: 'rgba(151,187,205,1)', data: [ 102, 123, 145, 60, 161] },{ label: 'my first dataset', fillcolor: 'rgba(220,220,220,0.2)', strokecolor: 'rgba(220,220,220,1)', pointcolor: 'rgba(220,220,220,1)', pointstrokecolor: '#fff', pointhighlightfill: '#fff', pointhighlightstroke: 'rgba(220,220,220,1)', data: [75, 59, 80, 81, 56, 55] } ] };
you can use $q service included in angular, allows create promises trigger functions in order of each other.
in first function need;
var deferred = $q.defer(); //stuff goes here function oncompletesuccess(){ //logic goes here deferred.resolve(); //you can pass variables if need be. } function onfail(){ return deferred.reject; }
to use can ;
$scope.masterfunction = function(var1, var2, var3){ $scope.loaddatafrommonth(var1, var2, var3).then(function(){ //can recieve variable if need $scope.loaddatatomontharivee(var1, var2, var3); } }
i noticed instantiate $scope variables twice, doing means overwrite defined $scope variables, overcome use "var [object]" create private object, push main.
more information on $q here
edit*
to merge json objects, remove following in second function;
$scope.data = {}; // new object $scope.data.datasets = []; // new array in data object .. $scope.data.labels =[];
then merge json objects, push newly made object created one;
$scope.data1.datasets.push($scope.data2.datasets[0]);
Comments
Post a Comment