• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WittySparks Logo White

WittySparks

Ignite Your Thoughts

  • Topics
  • Reviews
  • Newsletter
WittySparks / Technology / Programming / Avoid empty and duplicate records to get pushed in AngularJS
AngularJS Framework from Google

Avoid empty and duplicate records to get pushed in AngularJS

Programming June 22, 2013 by Sravan K

I am back again with a basic example on AngularJS on how to display the data from a set or array or JSON array, and how to add or insert or append data in an existing JSON array, and at the same time, how could we avoid empty data or duplicate records to get pushed into the JSON array.

This time I am back with Screencast Video tutorials or presentation whatever you call it is, and this is my first screen-cast video presentation too and that too in AngularJS; going forward, I will try to improve my voice and presentation skills, which will help me and you to get some online tutorials going forward.

Since this is my first video, I expect your valuable feedback on where I have to improve myself – which will help me grow further for a better presentation next time. I appreciate your feedback.

Ok – let me come back to this basic AngularJS tutorial; in this tutorial, you can see how to use the ngRepeat directive, forEach global API, and, while inserting data, how to avoid duplicate or empty data to get a push into the JSON array.

Here is the screen-cast video, which explains you well in detail in case you are a starter in AngularJS or about to learn AngularJS:

AngularJS – Basic example using ngRepeat and forEach

Demo | Download

JavaScript Code:

          var wittyApp = angular.module('wittyApp',[]);
          function AvoidDuplicateEntries($scope){
          $scope.movies = [
          {moviename: 'SuperMan'},
          {moviename: 'SpiderMan'},
          {moviename: 'BatMan'}
          ];
          var someMovie = $scope.movies;
          $scope.addMovie = function (){
          var newMovie = $scope.moviename;
          var oldmovies;
          if(newMovie){ //This will avoid empty data
          angular.forEach($scope.movies, function(eachmovie){ //For loop
          if(newMovie.toLowerCase() == eachmovie.moviename.toLowerCase()){ // this line will check whether the data is existing or not
          oldmovies = true;
          }
          });
          if(!oldmovies){
          someMovie.push({moviename:newMovie});
          }
          }
          }
          }

HTML Code:

<div style="padding:20px" ng-app="wittyApp" id="ng-app" ng-controller="AvoidDuplicateEntries">
<h1>Basic App using AngularJS</h1>
<table class="table table-bordered" style="width:220px;">
<thead>
<tr><td>#</td>
<td>Movie Name</td></tr>
</thead>
<tr ng-repeat='movie in movies'>
<td>{{$index+1}}</td>
<td>{{movie.moviename}}</td>
</tr>
</table>
<form ng-submit="addMovie();">
<input type="text" ng-model="moviename" size="30" placeholder="Which movie you want to watch" /><br />
<input type="submit" class="btn btn-primary" value="Add Movie">
</form>
</div>

Liked it? Appreciate your feedback about my first video and first example on AngularJS :)
More to come – keep visiting us!

Related Topics

  • PHP: Is it the best Programming Language ever?
  • How To Build A Great Search Box On Your Website
  • How to make Python3 version default in Windows OS?
Previous Post: « Dots a simple and interesting game for iOS
Next Post: Will Added Censorship Effect Future Google Glass Sales? »
Profile picture for Sravan K

About Sravan K

Contributor at WittySparks
WittySparks Staff

Sravan is a highly experienced web designer, user experience designer and developer. You can connect with him on Twitter or LinkedIn and initiate a chat.

View all posts by Sravan K

Primary Sidebar

Search

Exclusive Coupons

  • Moqups coupon code: WITTYSPARKS for 20% or PARTNERS50 for 50% discount.
  • WPForms coupon code: WITTYSPARKS for 50% off.
  • Serpstat coupon code: wittysparks_discount for 30% off.
  • SEO Buddy coupon code: WITTYSPARKS for 25% off.
  • Morningscore coupon code: wittysparks for 30% off for 3 months.
  • FlexClip coupon code: WITTYSPARKS for 30% off.
  • Uplead coupon code: “witty” for 30-day free trial.
  • FastestVPN coupon codes: WITTYSPARKS15 or WITTYSPARKS10 or Get up to 93% OFF.
  • Outranking.io coupon code: WITTYSPARKS50 for 50% off.

For more such offers visit our exclusive offers for SEO, Bloggers, Marketers and for Business owners.

Featured Productivity Software

Notion logo
Notion

Whether you’re a solo entrepreneur or a large team, Notion Workspace can help you stay organized and get more done. Get started today and take your productivity to the next level.

Try Notion for FREE

Footer

Affiliate Disclosure

If you make a purchase from WittySparks links, we will receive a small commission. See our Affiliate Disclosure.

Sponsors

Partnered with FreePik to use the licensed images.

turn to dhgate for smartphone

Follow Us

  • Facebook
  • Twitter
  • Pinterest
  • LinkedIn
  • Instagram
  • YouTube
  • RSS

Copyright © 2023 · Hosting sponsored by Rocket.net (Affiliate link)

  • About Us
  • Contact Us
  • Privacy Policy
  • LinkedIn
  • Twitter
  • Like
  • Pinterest