如何在Angular JS中将obj转换为字符串

时间:2013-12-07 09:08:11

标签: javascript json angularjs angularjs-factory

我试图找到工厂返回的剩余部分。即使obj返回一个数字,我仍然保持null。有没有办法将回报转换为真实数字?

使用路线提供者设置模块

            var emanuel = angular.module('emanuel', []).config(function($routeProvider){
                $routeProvider.when('/weekday-morning', {
                    templateUrl: 'content/weekday-morning.html',
                    controller: 'WeekdayMorning'
                });
                $routeProvider.otherwise ({redirectTo: '/home' });
            });

接收日期(mm / dd / yyyy)字符串并根据JSON日历解析它的工厂

            emanuel.factory('DayService', function($http, $q, $window){
                var obj = {};
                obj.oscWeek = function(d){
                    //receives a date format mm/dd/yyyy
                    var promise = $q.defer();
                    $http.get('content/calendar.json').success(function(data) {
                        var temp ='';
                        for (var i=0; i<data.calendar.seasons.season.length; i++){
                            //iterates through the end dates of all seasons to find current season
                            var day = new Date(d).getTime();
                            var end = new Date(data.calendar.seasons.season[i].end);
                            end.setHours(23,59);
                            //$window.alert(end);
                            end = end.getTime();
                            var diff = end - day;
                            diff = diff /(1000*60*60*24);
                            //$window.alert(diff);

                            if (parseFloat(diff) > 0){
                                // upon finding current season, find the time lapse since the start of the season
                                var start = new Date(data.calendar.seasons.season[i].start);
                                //$window.alert(start);
                                start = start.getTime();
                                var startDiff = day - start;
                                // converts time lapse into whole weeks
                                var week = parseInt(startDiff /(1000*60*60*24*7))+1;
                                promise.resolve(week);
                                break;
                            } 
                        }
                    });
                    return promise.promise;
                }
            return obj;
            });

接收obj返回的控制器。 temp返回1,但temp%2返回Null。

            emanuel.controller('WeekdayMorning', function($scope, DayService){
                    $scope.display = function(d) {
                        var date;
                        if(d=='today'){
                            date = new Date();
                        } else {
                            date = $scope.date;
                        }

                        var temp = DayService.oscWeek(date);
                        $scope.week = temp;
                        $scope.modulo = temp%2;
                    }
            });

1 个答案:

答案 0 :(得分:2)

您的服务方法会返回一个承诺。模运算符不适用于承诺。你应该在你的控制器中有这个:

temp.then(function(week) {
    $scope.week = week;
    $scope.modulo = week % 2;
});