编程语言
首页 > 编程语言> > javascript-如何在ionic(angular.js)中调用相同服务的其他功能

javascript-如何在ionic(angular.js)中调用相同服务的其他功能

作者:互联网

我正在使用离子框架开发移动应用程序.我已经创建了这样的通用实用程序服务

 .service('CommonUtilityService', function($q) {
        return {
            parseJsonDate:function(jsonDate){
                var offset = new Date().getTimezoneOffset() * 60000;
                var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);

                if (parts[2] == undefined)
                    parts[2] = 0;

                if (parts[3] == undefined)
                    parts[3] = 0;

                return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000);
            },
            daysBetween:function(date1String, date2String){
                var ONE_DAY = 1000 * 60 * 60 * 24;
                var ONE_MINUTE = 1000 * 60;

                var d1 = new Date(date1String);
                var d2 = new Date(date2String);
                var d1_ms = d1.getTime() - d1.getTimezoneOffset() * ONE_MINUTE;
                var d2_ms = d2.getTime() - d2.getTimezoneOffset() * ONE_MINUTE;

                return Math.floor(d1_ms - d2_ms/ONE_DAY);
            },
            getNumberOfDays:function(jsonDate){
               // var date = parseJsonDate(jsonDate);
                var date = new Date();
                var today = new Date();
                return this.daysBetween(today,date);
            }
        }
    })

当我尝试在getNumberOfDays函数内调用daysBetween函数时,出现错误

this.daysBetween is not a function

谁能告诉我如何在getNumberOfDays函数中调用daysBetween函数.

解决方法:

实际是Service doesn’t return a object where Factory return an object.您可以尝试像这样的重构服务

Service

A service is a constructor function which creates the object using the
new keyword. You can add properties and functions to a service object
by using the this keyword. Unlike a factory, it doesn’t return
anything (it returns an object which contains method).

 .service('CommonUtilityService', function($q) {
     var CommonUtilityService = this;
     CommonUtilityService.parseJsonDate = function(jsonDate) {
         var offset = new Date().getTimezoneOffset() * 60000;
         var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);

         if (parts[2] == undefined)
             parts[2] = 0;

         if (parts[3] == undefined)
             parts[3] = 0;

         return new Date(+parts[1] + offset + parts[2] * 3600000 + parts[3] * 60000);
     }
     CommonUtilityService.daysBetween = function(date1String, date2String) {
         var ONE_DAY = 1000 * 60 * 60 * 24;
         var ONE_MINUTE = 1000 * 60;

         var d1 = new Date(date1String);
         var d2 = new Date(date2String);
         var d1_ms = d1.getTime() - d1.getTimezoneOffset() * ONE_MINUTE;
         var d2_ms = d2.getTime() - d2.getTimezoneOffset() * ONE_MINUTE;

         return Math.floor(d1_ms - d2_ms / ONE_DAY);
     }
     CommonUtilityService.getNumberOfDays = function(jsonDate) {
         // var date = parseJsonDate(jsonDate);
         var date = new Date();
         var today = new Date();
         return CommonUtilityService.daysBetween(today, date);
     }
 })

标签:angularjs,ionic-framework,ionic,angularjs-service,javascript
来源: https://codeday.me/bug/20191028/1950805.html