angulartypescriptangularjs-ng-form

How to populate localstorage key value in Ng Field


I have a field that uses the Ng Directive for the required validation but i want to by default populate it with the value of my localstorage key localstorage.getItem('actreel');

FIELD

<div class="row">
   <div class="col-8">
    <input id="batchno" type="text" (ngModel)="model.reelno" name="reelno" #reelno="ngModel" placeholder="Reel No. *" value="" required>
   </div>
   <div class="col-12 rqderror" [hidden]="reelno.valid || reelno.untouched"> 
    Reel is required
   </div>
</div>

Solution

  • You should feel it in your ts controller in ngOnInit event like this :

     ngOnInit(){
         this.actreel = localstorage.getItem('actreel');
     }
    

    And then use it in your view field :

    <div class="row">
       <div class="col-8">
        <input id="batchno" type="text" [(ngModel)]="actreel" name="reelno" #reelno="ngModel" placeholder="Reel No. *" value="" required>
       </div>
       <div class="col-12 rqderror" [hidden]="reelno.valid || reelno.untouched"> 
        Reel is required
       </div>
    </div>