Kamyar Parastesh Kamyar Parastesh - 28 days ago 14
TypeScript Question

Template parse errors: Parser Error: Unexpected token 'Date'

I have a angular2 app with typescript. I am trying to get current time. Here is my code

<div>
<div class="row">
<div class="col-lg-6">
<div class="gotoAndBot-Copy-3" >
gotoAndBot
<span class="-copy-2">
{{new Date().toLocaleDateString()}}
</span>
</div>
</div>

</div>
</div>


but it complains with
Template parse errors: Parser Error: Unexpected token 'Date'

I am wundering why it does not recognize the
Date


so how can i fix it?

Answer

You can't place arbitrary expressions in an Angular 2 template. Instead, you can create a member in your component called dateString and display that.

Try

@Component({
    // ...
})
export class App {
    dateString = new Date().toLocaleDateString();
}

with the template

<div>
  <div class="row">
      <div class="col-lg-6">
          <div class="gotoAndBot-Copy-3" >
              gotoAndBot
          <span class="-copy-2">
            {{dateString}}
          </span>
          </div> 
      </div>
  </div> 
</div>
Comments