How to display date using angular.js? I tried to use angular-moment but I could not get a result.
- What date do you need to withdraw? Is the current date or in any one format, and do you want to output it in another? - LinnTroll
- The current need to withdraw. - spmpl
|
2 answers
You must first create a date object in the controller:
controller:
function Ctrl($scope) { $scope.date = new Date(); } conclusion:
<div ng-app ng-controller="Ctrl"> {{date | date:'yyyy-MM-dd'}} </div> - Displays on page {{date | date: 'yyyy-mm-dd'}} - spmpl
- jsfiddle.net/s6gy44ea here is my code. - spmpl
- @spmpl, you must use named applications. Demosyntax has been removed in 1.3. - Qwertiy ♦
- @spmpl, some kind of wrong code. No markup, no dates are not visible ... - Qwertiy ♦
|
How does it work for me ...
1) Service the locale. Example:
var App = angular.module("app", [ 'angularMoment' ]) .run([ '$rootScope', 'amMoment', function($rootScope, amMoment){ amMoment.changeLocale('ru'); }]) .controller('someCtrl',['$scope', function ($scope) { $scope.date = "2016-02-02 12:30:47"; /* some code */ }]); 2) In the template, specify the output format, in the example 'DD MMMM YYYY'
<body ng-app="app"> <div ng-controller="someCtrl"> <span ng-bind="date | amDateFormat:'DD MMMM YYYY'"></span> <span ng-bind="date | amDateFormat:'HH:mm:ss'"></span> </div> <body> Because You have the current date, you can try it.
In the controller:
$scope.currentDate = new Date(); In the template:
<span ng-bind="currentDate | amDateFormat:'DD MMMM YYYY HH:mm:ss'"></span> Check if the angular-moment is set https://github.com/urish/angular-angular
new Date()will not work, since the view is all executed in the context of scope, and it, by default, does not have aDatefield / method - Grundy- @Grundy, corrected) - Alexey Lemesh
- @spmpl, the problem is solved? - Alexey Lemesh
|