Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to find the remainder of a factory return. I keep getting null even though the obj is returning a number. Is there a way to convert the return to a true number?

Setting up module with route provider

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

the factory that receives date (mm/dd/yyyy) string and parses it against a JSON calendar

            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;
            });

The controller that receives the obj return. temp returns 1 but temp%2 returns 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;
                    }
            });
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Your service method returns a promise. The modulo operator doesn't apply to a promise. You should have this in your controller:

temp.then(function(week) {
    $scope.week = week;
    $scope.modulo = week % 2;
});
share|improve this answer
 
Perfect. Thanks for the help. –  Alen Giliana 45 mins ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.