Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. mainApp.controller('homeController', ['$scope', function($scope) {
  2.  
  3.  
  4.   $scope.contactList = [];
  5.   $scope.contact = [];
  6.  
  7.  
  8.   if(localStorage.contactList)     
  9.     $scope.contactList = JSON.parse(localStorage.contactList);
  10.  
  11.  
  12.   $scope.reload = function(){
  13.  
  14.     $scope.contactList = JSON.parse(localStorage.contactList);
  15.  
  16.   }
  17.  
  18.  
  19.  
  20.   $scope.addContact = function (){
  21.  
  22.     if($scope.contact.name && $scope.contact.tel && $scope.contact.mail){
  23.  
  24.           var contact = {
  25.             name    : $scope.contact.name,
  26.             tel     : $scope.contact.tel,
  27.             mail    : $scope.contact.mail
  28.             }
  29.  
  30.         $scope.contactList.push(contact);
  31.         //console.log($scope.contactList);
  32.  
  33.         $scope.contact.name = "";
  34.         $scope.contact.tel = "";
  35.         $scope.contact.mail = "jbdfg@gmail.com";
  36.         $scope.contact.mail = "";
  37.  
  38.         $scope.msg ="";
  39.         localStorage.contactList = JSON.stringify($scope.contactList);
  40.  
  41.  
  42.     }else{
  43.         $scope.msg ="All fields are required";
  44.     }
  45.  
  46.   }
  47.  
  48.   $scope.deleteContact = function ($index){
  49.  
  50.     if(confirm("Are you sure you to delete this contact ?")){
  51.         $scope.contactList.splice($index,1);  
  52.         localStorage.contactList = JSON.stringify($scope.contactList);     
  53.     }
  54.  
  55.   }
  56.  
  57.   $scope.editContact = function ($index){
  58.  
  59.      $scope.contact.name = $scope.contactList[$index].name;
  60.      $scope.contact.tel = $scope.contactList[$index].tel;
  61.      $scope.contact.mail = $scope.contactList[$index].mail;
  62.      $scope.contact.index = $index;
  63.  
  64.      $scope.isEdit = true;
  65.   }
  66.  
  67.   $scope.updateContact = function ($index){
  68.     console.log($index);
  69.  
  70.         var contact = {
  71.             name    : $scope.contact.name,
  72.             tel     : $scope.contact.tel,
  73.             mail    : $scope.contact.mail
  74.         }
  75.  
  76.         $scope.contactList[$index] = contact;
  77.         //console.log($scope.contactList);
  78.  
  79.         $scope.contact.name = "";
  80.         $scope.contact.tel = "";
  81.         $scope.contact.mail = "";
  82.         $scope.contact.index = "";
  83.  
  84.         localStorage.contactList = JSON.stringify($scope.contactList);
  85.  
  86.   }
  87.  
  88.   $scope.cancelEdit = function(){
  89.         $scope.isEdit = false;
  90.         $scope.contact.index = "";
  91.   }
  92.  
  93. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement