Home Blog Work

Commenting system using Angular.js

Published on April 29, 2015

Hi guys, I’m back with another angular.js application. A commenting system you might have seen this before other technologies but now we will create using angular.js. It took 1 hr to do this but I’m sure it’s bit hard to split and explain. I try to do my best please bare with me.

Demo download

Before we start. I’m using semantic UI for my CSS designs. Angular ngMessges with semantic error classes to show form validation message. Finally, to generate md5 hash keys out of user email enter by user. We need a md5 hash of email to get gravatar profile image. If you don’t know what is gravatar, it’s a service to get user profile pic based on user email. If a user does not register with gravatar it will give you default image. Ok. Now let’s jump in coding part.


<!DOCTYPE html>
<html lang="en" data-ng-app="Commenter">
    <meta charset="UTF-8" />
    <title>Comment system using angular js - techumber</title>
    <div class="ui page grid">
      <div class="column">
        <h1 class="ui header align cetner">NG-Commenter</h1>
    <script src="//"></script>
    <script src="//"></script>
    <script src="//"></script>
    <script src="app.js"></script>

html boilder with angularjs, angular-messages, md5.js and semanticui.


<div class="ui divided list" data-ng-cloak>
  <div class="item" data-ng-repeat="comment in comments">
    <img class="ui avatar image" data-ng-src="//{{comment.avatar}}" />
    <div class="content">
      <a class="header">{{comment.username}} </a>
      <div class="description">{{comment.text}}</div>

Here, we are using the angular ng-repeate directive to loop through all comments. A user will enter a username, email, and text values but the avatar we need to generate it. We will see it later but for now, we simply displaying the values in a list.


<form class="ui form" novalidate data-ng-submit="addComment(cmt)" name="commentForm">
  <button type="submit" class="ui submit teal button" data-ng-disabled="commentForm.$invalid">Submit

HTML form with the angular ng-submit directive. novalidate disables html5 validations. We are disabling submit button until all input controls are valid. When we submit the form we will call addComment() function. It’s always a good thing to use ng-submit instead of ng-click on submit button. ng-submit supports both pressing enter key as well as clicking submit button.


  class="required field"
  data-ng-class="{error: (commentForm.username.$invalid && commentForm.username.$dirty)}"
  <label>Name </label>
  <div class="ui icon input">
    <input type="text" placeholder="Name" name="username" data-ng-model="cmt.username" required />
    <i class="user icon"> </i>
  <div ng-messages="commentForm.username.$error">
    <div data-ng-include="'messages.tpl.html'"></div>

We are using ng-class to add error class, if the control is invalid and it’s been used at least once. We are using ng-messages directive to show error messages. I have created messages.tpl.html which will contain all possible error messages. We can reuse that template for other controls.


<div class="ui red pointing prompt label transition visible" ng-message="required">
  This filed is required
<div class="ui red pointing prompt label transition visible" ng-message="email">
  Enter Valid email

This messages template will contain all error messages. We are using semantic UI class to decorate the error messages.


angular.module('Commenter', ['ngMessages']).controller('CommentManageCtrl', [
  function($scope) {
    $scope.comments = [];
    $scope.addComment = function(cmt) {
      cmt.avatar = md5(;

Finally, we are at app.js. It’s a very simple code. When a user submits form we are calling addComment function. Inside we are generating md5 hash value. We are creating avatar property on the object and adding them to comments property. Whenever any variable on the scope it will automatically update on the frontend.