Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
185
rated 0 times [  186] [ 1]  / answers: 1 / hits: 21496  / 8 Years ago, thu, january 5, 2017, 12:00:00

In a fairly complicated angular web app, I need the dynamic year to show up in a footer that doesn't have a directive. Is there an angular way to do it using just {{SomeDateFunction()}}?



I've seen people say {{Date.now() | date:'yyyy'}}, which doesn't display anything. I've tried {{new Date().getFullYear()}} which breaks angular and the error says




Syntax Error: Token 'Date' is an unexpected token at column 5 of the expression [new Date().getFullYear()] starting at [Date().getFullYear(].




I don't want to link a scope from the root scope, or put this in a directive just for the year, this is exactly the kind of thing {{ }} expressions should be able to solve, and non-date related simple {{ }} expressions display just fine.


More From » angularjs

 Answers
4

You need to define the date object inside the controller onto the scope.



$scope.date = new Date();


And then in view do



{{date| date:'yyyy'}}

[#59449] Tuesday, January 3, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nadiatristinl

Total Points: 151
Total Questions: 116
Total Answers: 108

Location: Japan
Member since Tue, Jul 26, 2022
2 Years ago
nadiatristinl questions
Tue, Mar 15, 22, 00:00, 2 Years ago
Wed, Dec 29, 21, 00:00, 2 Years ago
Thu, Dec 17, 20, 00:00, 4 Years ago
Sat, Jul 11, 20, 00:00, 4 Years ago
Wed, Apr 29, 20, 00:00, 4 Years ago
;