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

标签 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 返回的 Controller 。 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;
                    }
            });

最佳答案

您的服务方法返回一个 promise 。模运算符不适用于 Promise。你的 Controller 中应该有这个:

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

关于javascript - 如何在 Angular JS 中将 obj 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20439613/

相关文章:

jquery - 在codeigniter(flashdata)中返回一个json对象

css - 专门为徽章应用一个类

AngularJS $routeProvider : routing does not work

javascript - 使用 Javascript 检索 Google Analytics 跟踪器名称时出现问题

javascript - 当绑定(bind)到深度嵌套的属性时,Ember 计算属性不会触发

json - 显示 JSON 数组数据时出错 - 无法将复杂对象类型转换为简单值

json - 如何使用 Play Framework 2.1-RC2 中新的 JSON API 读写 Anorm 对象?

java - 将 AngularJS 转换为 Word 文档

javascript - 未捕获的类型错误 : undefined is not a function (anonymous function) in Wordpress

javascript - 轴未与钢筋对齐