在angularjs中的另一个函数内声明函数 [英] Declare function inside another function in angularjs

查看:165
本文介绍了在angularjs中的另一个函数内声明函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的第一个功能:

 $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 to push into 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 to GET DATA----
    ajaxServicess.getData(url,username,password, 'GET', '').then(onSuccess, onError);
};

此函数返回以下结果:

$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]
        }
    ]
};

效果不错。

此第二个函数返回不同的数据:

I have this second function that 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 to push into 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 to GET DATA----
    ajaxServicess.getData(url,username,password, 'GET', '').then(onSuccess, onError);

};

此函数返回结果:

 $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 good also, but my question is: How I can declare the second function inside the first one and combine the data returned and get the final result like 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]
        }
    ]
};


推荐答案

在您的第一个函数中,您将需要;

In your first function you will need;

var deferred = $q.defer();
//stuff goes here
function onCompleteSuccess(){
     //logic goes here
     deferred.resolve(); //you can pass back variables too if need be.
}
function onFail(){
    return deferred.reject;
}

要使用此功能,您可以;

To use this you can ;

$scope.masterFunction = function(var1, var2, var3){
    $scope.loadDataFromMonth(var1, var2, var3).then(function(){ //can recieve a variable if you need to
         $scope.loadDataToMonthArivee(var1, var2, var3);
    }  
}

我也注意到你实例化你的$ scope变量两次,这意味着你将覆盖以前定义的$ scope变量,克服这个使用var [object] 创建一个私人对象,然后你会推送到你的主要。

I also noticed that you instantiate your $scope variables twice, doing this means that you will overwrite your previously defined $scope variables, to overcome this use "var [object]" to create a private object, which then you would push to your main.

有关 $ q here

编辑*

要合并JSON对象,请在第二个函数中删除以下内容:

To merge your JSON objects, remove the following in your second function;

$scope.data = {}; // new object
$scope.data.datasets = []; // new array in data object ..
$scope.data.labels =[];

然后,要合并JSON对象,只需将新创建的对象推送到当前创建的对象;

Then to merge JSON objects, just push your newly made object to the currently created one;

$scope.data1.datasets.push($scope.data2.datasets[0]);

这篇关于在angularjs中的另一个函数内声明函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆