在AngularJS应用程序中使用服务器日期的方法 [英] Ways to use server date in an AngularJS application

查看:46
本文介绍了在AngularJS应用程序中使用服务器日期的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种不错的"方式将服务器的日期设置为angularJS应用?

Is there a "nice" way to set the server's date to an angularJS app?

假设我有一个API路由来获取此信息,例如/api/date,我可以使用名为dateService的角度服务来调用它:

Let's say that I have an API route to get this, sort of /api/date and I can call it with an angular service called dateService:

angular.module('myApp').service('DateService', ['$resource', function ($resource) {
    return $resource('/api/date', { }, {
    });

}]);

我需要在应用启动后立即获取日期,因为我的html文件使用基于该日期的功能来显示某些内容.否则,每次调用都会出现未定义"错误.

I need to get the date as soon as the app starts because my html file uses a function based on that date to display something. Otherwise I'll get an "undefined" error everytime I call it.

预先感谢

推荐答案

您可以在app.run中完成

You can do that in app.run

var app = angular.module('myApp',[]);
app.run(function ($rootScope, $state, DateService) {

            // call date service and save it in $rootScope

        });

这篇关于在AngularJS应用程序中使用服务器日期的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆