AngularJS ngInit with Date [英] AngularJS ngInit with Date

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

问题描述

我在.NET MVC中进行编码,并希望将存储的DateTime对象传递给前端,作为控制器中Date对象的初始值。



这是我试过的:

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

这似乎不起作用,我收到语法错误。



如何传递一个Date对象作为范围变量的初始属性?



http://jsfiddle.net/xX8LJ/



更新



我忘了在我的代码示例中包含Razor代码。

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


解决方案

角度范围不知道 Date



尝试使用:

  $ scope.Date = new Date(); 

之后:

 < divng-controller =myController
ng-init =params.datetime = Date
>当前时间:{{params.datetime}}< / div>

演示1 小提琴



作为您编辑的答案,



重写日期作为方法:

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

和:

 code>< divng-controller =myController
ng-init =params.datetime = Date('Tue Feb 18 2013 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

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

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