Wednesday, April 30, 2014

angularjs adding values and avoid duplicate entries:

<!doctype html>
<html lang="en" ng-app="wittyapp">
<head>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.min.js"></script>
<script src="http://code.angularjs.org/angular-resource-1.0.0.min.js"></script>
</head>
<body>
<div  ng-controller="ContactController">
   
    Email:<input type="text" ng-model="moviename" />
   
    <button ng-click="addmovie()">Add</button>
   
    <h2>Contacts</h2>
    <ul>
<li ng-repeat="movie in movies"> {{ movie.moviename }} </li>
</ul>
   
</div>

<script>
var wittyapp=angular.module("wittyapp",[]);
function ContactController($scope) {
    $scope.movies = [{moviename:'viralpatel.net@gmail.com'}, {moviename:'hello@email.com'}];
    var somemovie=$scope.movies;
    $scope.addmovie = function() {
    var newmovie=$scope.moviename;
    var oldmovie;
    angular.forEach($scope.movies,function(eachmovie){
    if(newmovie.toLowerCase()==eachmovie.moviename.toLowerCase()){
    oldmovie=true;
    //document.writeln("same value exist");
    alert("same value exist");
    }
   
    /*else{
   
     somemovie.push({moviename:newmovie});
       $scope.moviename = "";
       } */
   
    });
    if(!oldmovie){
   
     somemovie.push({moviename:newmovie});
       $scope.moviename = "";
       }

}

}

</script>

</body>
</html>

2 comments:

  1. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly
    Angularjs Training In Hyderabad

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete