Saturday, 6 June 2015

Add & Remove Functionality

Now, let me show you how to add any item in Angular JS and by clicking on the remove button, the corresponding item will be erased.


add_delete.html:-
<html ng-app="nameApp">
  <head>
    <meta charset="utf-8">
    <title>Angular.js Example</title>
    <script src="angular.min.js"></script>
    <script>
      var nameApp = angular.module('nameApp', []);
      nameApp.controller('NameCtrl', function ($scope){
        $scope.names = ['Larry', 'Curly', 'Moe'];

        $scope.addName = function() {
          $scope.names.push($scope.enteredName);
          $scope.enteredName = '';
        };

        $scope.removeName = function(name) {
          var i = $scope.names.indexOf(name);
          $scope.names.splice(i, 1);
        };
      });
    </script>
  </head>
  <body ng-controller="NameCtrl">
    <ul>
      <li ng-repeat="name in names">{{name}}
      <a href="" ng-click="removeName(name)">remove</a></li>
    </ul>
    <form ng-submit="addName()">
      <input type="text" ng-model="enteredName">
      <input type="submit" value="add">
    </form>
  </body>
</html>


Screenshots:-






Explanation:-

The names are typed in the text box and then clicked on the "add" button to display it on the screen. When the end-user will click on the "remove" link, the corresponding items will be deleted.

No comments:

Post a Comment