Question: how to get the current year in Angular 1 using just {{ }}?

Question

how to get the current year in Angular 1 using just {{ }}?

Answers 1
Added at 2017-01-05 18:01
Tags
Question

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.

Answers
nr: #1 dodano: 2017-01-05 18:01

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'}}
Source Show
◀ Wstecz