Uncategorized

Day # 13 Connecting our application to a REST API using ngResource

Promises in AngularJS with $q service :-

Yesterday we were able to create the form and save it. But we want to exit the form once we click on save button. form is actually retrieved from createModal of showCreateModal scope variable which has the template. We need to call the hide function from createModal.  we will create the promise function to hide the form and for this purpose we use $q which is a support parameter to $promise.


//trimmed code

app.service('ContactService', function (Contact, $q) {

//trimmed code

//trimmed code

'createContact': function (person) {
 var d = $q.defer();
 self.isSaving = true;
 Contact.save(person).$promise.then(function () {

 self.isSaving = false;
d.resolve()
});

return d.promise;

}

//trimmed code

And in the createContact we will hide the modal.

 

As you could notice in the above screencast, we will make sure we clean the contact list which will not spread onto contactDetail once we save the save the form.


//trimmed code createContact service

Contact.save(person).$promise.then(function () {
 self.isSaving = false;
 self.selectedPerson = null;
 self.hasMore = true;
self.page = 1;
 self.persons = [];
 self.loadContacts();
 d.resolve()

});

//trimmed code

code :- https://github.com/praneethkumarpidugu/angular1-core/commit/fd837b7d4f71b02e08032b6e4724414194036f7d

Leave a comment