0

I have an Array of Elements of the type ActionInstruction. Some get added in the constructor of the component, others are added dynamically by the user via a dialog form field.

 actionInstructions: ActionInstruction[];

 constructor(private _formBuilder: FormBuilder, public dialog: MatDialog) {
 this.actionInstructions = [{post_date_start: new Date()}];
 }

 someMethod(){
    // get result of Type ActionInstruction from Dialog -> newInstruction
  this.actionInstructions.push(newInstruction);
 }

The content of the array gets displayed in the view like follows:

<div *ngFor="let actionInstruction of actionInstructions">
      <div *ngIf="actionInstruction.post_date_start"   (click)="openActionCreation(actionInstruction)" class="action-instruction">
         <div class=" fl ml-50">
          Am {{actionInstruction.post_date_start.toLocaleDateString()}}
         </div> 
      </div>
</div>

At start the elements that are added in the constructor, are getting with the localized date without any problem. But after adding an element dynamically via the form, the following error gets thrown:

Error Output

At first I thought it could be caused by calling it during runtime in the view, but the problem actually occures in another totally independent component, where I prepare the variable to be shown in the view, in the constructor with the same function.

 constructor(){
   if(this.user.birthday != null){
        this.localizedBirthday = this.user.birthday.toLocaleDateString();
    }
  }

Here it seems allmost patternless when the error occures and when not. The only thing that both components have in common, that they use some *ngIf pattern in the view component, where the date get´s displayed.

1 Answer 1

2

Not a direct solution but you could use the datepipe in your components. https://angular.io/api/common/DatePipe

it would look something like this:

<div class=" fl ml-50">
  Am {{actionInstruction.post_date_start | date:'medium' }}
</div> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.