title: How to get localStorage value in Angular 2 template
body: <p>Here is my code,</p>
  
  <p>HTML:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;div *ngFor="let comment of comments | orderBy: '-'+orderBy"&gt;
      &lt;div class="comment-item"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-md-8"&gt;
                  {{comment.userName}}
              &lt;/div&gt;
              &lt;div class="col-md-4"&gt;
                  &lt;div class="comment-timeago"&gt;
                      {{comment.time | timeAgo}}
                  &lt;/div&gt;
                  &lt;div class="likedicon"&gt;
                      &lt;i class="fa fa-heart-o disliked" aria-hidden="true" (click)="likeComment(comment._id, comment)"&gt;&lt;/i&gt;
                      &lt;i class="fa fa-heart liked" aria-hidden="true" *ngIf="comment.liked == 'liked'" (click)="dislikeComment(comment._id, comment)"&gt;&lt;/i&gt;
                  &lt;/div&gt;
                  &lt;div class="like-num"&gt;
                      {{comment.like}}
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>File component.ts:</p>
  
  <pre class="lang-typescript prettyprint-override"><code>likeComment(commentId, comment) {
  
      localStorage[commentId] = "liked";
      comment.liked = localStorage[commentId];
      comment.like ++;
      this.dataService.likeComment(commentId).subscribe(
          // Post data to server
          error =&gt; console.log(error)
      )
  }
  
  dislikeComment(commentId, comment) {
      localStorage[commentId] = "disliked";
      comment.liked = localStorage[commentId];
      comment.like --;
      this.dataService.dislikeComment(commentId).subscribe(
          // Post data to server
          error =&gt; console.log(error)
      )
  </code></pre>
  
  <p>Every comment can switch liked or disliked independently, but I have to use localStorage to decide what status should display. Maybe like:</p>
  
  <pre><code>*ngIf="localStorage.getItem(comment._Id) == 'liked'"
  </code></pre>
  
  <p>Unfortunately, it's the wrong syntax. I find the getter working on my other case, and the reference is from <em><a href="https://stackoverflow.com/questions/42087098/angular2-how-to-display-localstorage-value-inside-html5-template">Angular2 How to display localStorage value inside HTML5 template?</a></em>.</p>
  
  <p>In this case, I have no idea to do that, because I can't use get commentLike() in my function, neither use a global variable. How do I solve it?</p>