AngularJS ngInit与日期日期、AngularJS、ngInit

由网友(不爱又何必纠缠)分享简介:我编码.NET MVC和想存储DateTime对象传递到前端,作为在控制器Date对象的初始值。下面是我已经试过: NG-的init =params.datetime =新的Date() 这似乎并没有工作,我得到一个语法错误。我怎么能在一个Date对象传递作为一个范围的变量的初始属性? http://jsfiddl...

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

下面是我已经试过:

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

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

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

AngularJS快速入门

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

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

演示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

阅读全文

相关推荐

最新文章