AngularJS ngInit与日期 [英] AngularJS ngInit with Date

查看:966
本文介绍了AngularJS ngInit与日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码.NET MVC和想存储DateTime对象传递到前端,作为在控制器Date对象的初始值。

下面是我已经试过:

  NG-的init =params.datetime =新的Date()

这似乎并没有工作,我得到一个语法错误。

我怎么能在一个Date对象传递作为一个范围的变量的初始属性?

http://jsfiddle.net/xX8LJ/

更新

我忘了,包括我的code样品中的剃刀code。

  NG-的init =params.datetime =新的日期('@ Model.DepartureTime.ToString(S)')


解决方案

角范围不知道日期

尝试使用:

  $ scope.Date =新的日期();

和后:

 < D​​IV NG控制器=myController的
     NG-的init =params.datetime = DATE
 >当前时间:{{params.datetime}}< / DIV>

演示1 <大骨节病> 小提琴

至于你编辑的答案,

重写日期的方法:

  $ scope.Date =功能(ARG){
   返回新的日期(ARG);
};

 &LT; D​​IV NG控制器=myController的
      NG-的init =params.datetime = DATE('星期二2013年2月18日00:00:00 GMT + 0200(CEST)')
  &GT;当前时间:{{params.datetime}}&LT; / DIV&GT;

演示2 <大骨节病> 小提琴

I'm coding in .NET MVC and would like to pass a stored DateTime object to the front-end as an initial value for a Date object in a controller.

Here's what I've tried:

ng-init="params.datetime = new Date()"

This doesn't seem to work, and I'm getting a syntax error.

How can I pass in a Date object as an initial property for a scope variable?

http://jsfiddle.net/xX8LJ/

UPDATE

I forgot to include the Razor code in my code sample.

ng-init="params.datetime = new Date('@Model.DepartureTime.ToString("s")')"

解决方案

Angular scope doesn't know about Date

Try to use:

 $scope.Date = new Date();

and after:

<div ng-controller="myController"
     ng-init="params.datetime = Date"
 >Current time: {{params.datetime}}</div>

Demo 1 Fiddle

As an answer for your EDIT,

rewrite Date as method:

$scope.Date = function(arg){
   return new Date(arg);
};

and:

 <div ng-controller="myController" 
      ng-init="params.datetime = Date('Tue Feb 18 2013 00:00:00 GMT+0200 (CEST)')"
  >Current time: {{params.datetime}}</div>

Demo 2 Fiddle

这篇关于AngularJS ngInit与日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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