简体   繁体   English

另一个函数内部的Ionic Call函数(JS服务)

[英]Ionic Call function inside another function (JS service)

Hey guys so I have the following question : I'm busy with an Ionic mobile app for a project and I need to call a function in another function in one of my services .js Files (pushNotificationService.js) 大家好,我有以下问题:我正在为项目使用Ionic移动应用程序,因此需要在我的一个服务.js文件(pushNotificationService.js)中的另一个函数中调用一个函数

here is the code : 这是代码:

            checkForNewMessage: function () {
            console.log("This is the regID : " + regID);
            console.log($rootScope.regID);

            var data =
            {
                "action": "push_messages",
                "key": systemKey,
                "data":
                {
                    "action": "checkNewMessages",
                    "device_id": regID,
                    "key": systemKey
                }

            };

            console.log(json_encode(data));
            return $http.post(webserviceURL, json_encode(data))
                .then(function (response) {
                    getMsgNotiLocation(); //this is the function not working
                    return response;
                });
            // getMsgNotiLocation();
        },

EDIT : here is the getMsgNotiLocation(): 编辑:这是getMsgNotiLocation():

            getMsgNotiLocation: function () {
            $rootScope.showCounter = true;
            //console.log("This is what gets returned with the regID : " + json_decode(JSON.stringify(window.localStorage.getItem(regID))));
            var theMessagePage = json_decode(window.localStorage.getItem(regID));
            for (var x in theMessagePage) {
                if (theMessagePage[x].read != "true") {
                    console.log("We have gotten this far atleast : " + JSON.stringify(theMessagePage[x].page))
                    if (theMessagePage[x].page === "messages") {
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log(" Afkondigings Message recieved and message count : " + JSON.stringify($rootScope.afkondigMsgCount));
                    }
                    else if (theMessagePage[x].page === "calendar") {
                        $rootScope.kalenderMsgCount = $rootScope.kalenderMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log(" Calendar Message recieved and message count : " + JSON.stringify($rootScope.kalenderMsgCount));
                    }
                    else if (theMessagePage[x].page === "sponsor") {
                        $rootScope.sponsorMsgCount = $rootScope.sponsorMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("SPONSOR Message recieved and message count : " + JSON.stringify($rootScope.sponsorMsgCount));
                    }
                    else if (theMessagePage[x].page === "#/app/schools/2/Academics") {
                        $rootScope.akademieMsgCount = $rootScope.akademieMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("Akademie Message recieved and message count : " + JSON.stringify($rootScope.akademieMsgCount));
                    }
                    else if (theMessagePage[x].page === "#/app/schools/3/Sport") {
                        $rootScope.sportMsgCount = $rootScope.sportMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("Sport Message recieved and message count : " + JSON.stringify($rootScope.sportMsgCount));
                    }
                    else if (theMessagePage[x].page === "#/app/schools/4/Culture") {
                        $rootScope.kultuurMsgCount = $rootScope.kultuurMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("Kultuur Message recieved and message count : " + JSON.stringify($rootScope.kultuurMsgCount));
                    }
                    else if (theMessagePage[x].page === "contact") {
                        $rootScope.kontakMsgCount = $rootScope.kontakMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("Kontak Message recieved and message count : " + JSON.stringify($rootScope.Kontak));
                    }
                    else if (theMessagePage[x].page === "#/app/schools/5/Information") {
                        $rootScope.inligtingMsgCount = $rootScope.inligtingMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("Inligting Message recieved and message count : " + JSON.stringify($rootScope.inligtingMsgCount));
                    }
                    else if (theMessagePage[x].page === "news") {
                        $rootScope.downloadsMsgCount = $rootScope.downloadsMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("Aflaaibaar Message recieved and message count : " + JSON.stringify($rootScope.downloadsMsgCount));
                    }
                    else if (theMessagePage[x].page === "#/app/schools/1/Applications") {
                        $rootScope.aansoekMsgCount = $rootScope.aansoekMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("Aansoekvorms Message recieved and message count : " + JSON.stringify($rootScope.aansoekMsgCount));
                    }
                    else if (theMessagePage[x].page === "wallet") {
                        $rootScope.beursieMsgCount = $rootScope.beursieMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("Beursie Message recieved and message count : " + JSON.stringify($rootScope.beursieMsgCount));
                    }
                    else if (theMessagePage[x].page = "affiliates") {
                        $rootScope.besigheidMsgCount = $rootScope.besigheidMsgCount + 1;
                        $rootScope.afkondigMsgCount = $rootScope.afkondigMsgCount + 1;
                        $rootScope.msgcount = $rootScope.msgcount + 1;
                        console.log("Besigheidsforum Message recieved and message count : " + JSON.stringify($rootScope.besigheidMsgCount));
                    } else {
                        console.log("Everything failed please recheck the if statements");
                    }

                } else {
                    //The same as if the condition is met but just a - instead of a + 
                }
            }
        },

The other function works 100% (getMsgNotiLocation()) because it gets called elsewhere as well. 另一个函数工作100%(getMsgNotiLocation()),因为它也在其他地方被调用。 When I try to execute the app I get the following error : ReferenceError: getMsgNotiLocation is not defined 当我尝试执行该应用程序时,出现以下错误:ReferenceError:未定义getMsgNotiLocation

I just can't figure out how to do this please any help will be appreciated thanks in advance ! 我只是不知道该怎么做,请任何帮助将不胜感激,谢谢!

Also I know all the comments between the code is bad and stuff but before release I clean my code nicely its just to keep track where I've done what 我也知道代码之间的所有注释都是不好的,但是在发布之前,我很好地清理了代码,只是为了跟踪我在哪里做了什么

May be you can trying using arrow function 也许您可以尝试使用箭头功能

return $http.post(webserviceURL, json_encode(data))
                .then((response)=> {
                    this.getMsgNotiLocation(); //this is the function not working
                    return response;
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM