Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a contact Form in my App. I am adding email field dynamically when user clicks on add Email button. if user may or may not enter the value after email field generated when he clicks on add Email button.

If user not enter the value of Email and he clicks on submit, the angularjs sending data as emails[{"key":"Work","value":"[email protected]"},{"key":"","value":""}] to server.

How to remove these type of empty objects (like {"key":"","value":""}) while submitting the form to server.

share|improve this question
    
just edit your object before sending and delete items with no value... –  wickY26 Jun 13 at 7:47
add comment

1 Answer

up vote 2 down vote accepted

Just before send check if if email.value is "" and remove it from array there jsbin :

$scope.send = function() {

    angular.forEach($scope.emails, function(email, index){

     if(email.value ===""){
       $scope.emails.splice(index, 1);

     } 

    });
share|improve this answer
    
I have added multiple empty values but its deleting last value and coming out of loop. why?? –  chandu Jun 13 at 9:45
    
jsbin.com/waxenusa/4/edit –  sylwester Jun 13 at 10:24
    
or this solution if you don't want create extra array jsbin.com/waxenusa/10/edit –  sylwester Jun 13 at 10:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.