Skip to content

Instantly share code, notes, and snippets.

@pankajpatel
Last active August 29, 2015 14:22
Show Gist options
  • Save pankajpatel/3e7313538be9f3e9d2cd to your computer and use it in GitHub Desktop.
Save pankajpatel/3e7313538be9f3e9d2cd to your computer and use it in GitHub Desktop.
Complete controllers.js for contact store application
app
.controller('ContactsController', ['storage', '$scope', '$window', function(storage, $scope, $window){
$scope.contacts = [];
if( $scope.contacts.length == 0){
$scope.contacts = JSON.parse( storage.recall( 'contacts' ) );
}
$scope.deleteContact = function (index) {
var areYouSure = $window.confirm('Are you sure about deleting this contact?');
if( areYouSure ){
$scope.contacts.splice( index, 1);
storage.memorize( 'contacts', JSON.stringify( $scope.contacts ) );
}
}
}])
.controller('ContactController', ['storage', '$scope', '$routeParams', function(storage, $scope, $routeParams){
$scope.contact = {};
$scope.contacts = [];
if( $scope.contacts.length == 0){
$scope.contacts = JSON.parse( storage.recall( 'contacts' ) );
}
if( typeof $routeParams.index != 'undefined' ){
$scope.contact = $scope.contacts[$routeParams.index];
}
}])
.controller('EditContactController', ['storage', '$scope','$window','$location','$routeParams', function(storage, $scope, $window, $location, $routeParams){
$scope.contact = {};
$scope.contacts = [];
$scope.editMode = false;
if( $scope.contacts.length == 0){
$scope.contacts = JSON.parse( storage.recall( 'contacts' ) );
}
if( typeof $routeParams.index != 'undefined' ){
console.log( 'Edit Mode' );
$scope.editMode = true;
$scope.contact = $scope.contacts[$routeParams.index];
}
$scope.addContact = function () {
if( $scope.editMode ){
$scope.contacts[$routeParams.index] = $scope.contact;
} else {
$scope.contacts.push( $scope.contact );
}
storage.memorize( 'contacts', JSON.stringify( $scope.contacts ) );
contactForm.reset();
$scope.contact = {};
var more = $window.confirm('Want to Add more Contacts?');
if( !more ){
$location.path('/contacts');
} else {
if( $scope.editMode ){
$location.path('/add');
}
}
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment