Search code examples
angularjsformsemailhtml-email

Add several emails and diplay defined values


My form allows to update and add several emails. The first email field (main email) is mandatory. After filling it's possible to click on a button "Add email" for adding a new email address. By this way it's possible to add 4 others emails (5 emails in total).

The system checks if the format of the email is correct and displays a message if necessary and register the data in a DB.

Here my view (manageContact.html)

  <div class="panel-body">
    <form name="ContactForm" class="form-horizontal" role="form" novalidate ng-submit="submitForm(contact)">

    <!---------------- FOR UPDATING EMAILS FIELDS ------------ START --->   

    <div ng-repeat="(key, email) in emails | limitTo : 5" style="z-index:6">

      <div class="form-group">

        <span ng-switch="$index">
            <label ng-switch-when="0" for="txtEmail" class="col-sm-2 control-label">Main email</label>
            <label ng-switch-default for="txtEmail" class="col-sm-2 control-label">Email {{$index+1}}</label>
        </span> 

        <div class="col-sm-9" ng-switch="$index">

            <input ng-switch-when="0" type="email" class="form-control"
            name="txtEmail_{{$index}}" maxlength="100" placeholder="Enter main email"
            ng-model="contact.EMAIL">       

            <input ng-switch-default type="email" class="form-control" 
            name="txtEmail_{{$index}}" maxlength="100" placeholder="Enter Email {{$index+1}}" 
            ng-model="contact['EMAIL_'+$index]">    

            <div class="error-container" 
             ng-show="ContactForm['txtEmail_' + $index].$dirty && ContactForm['txtEmail_' + $index].$invalid">
                <div ng-show="ContactForm['txtEmail_' + $index].$error.email" class="alert alert-info" role="alert" style="margin-top:10px;">
                  <span class="glyphicon glyphicon-alert" aria-hidden="true"></span>
                  <span class="sr-only">Error:</span>
                  That is not a valid email. Please input a valid email.
                </div>

                <div ng-show="ContactForm['txtEmail_' + $index].$error.required" class="alert alert-info" role="alert" style="margin-top:10px;">
                  <span class="glyphicon glyphicon-alert" aria-hidden="true"></span>
                  <span class="sr-only">Error:</span>
                  Your email is required.
                </div>

            </div>

        </div>

        <div  class="col-sm-1" ng-show="$index == 0">
            <a href="" ng-click="add()" ng-show="emails.length<5" class="inline btn btn-primary icon_email">
            <span class="glyphicon glyphicon-plus icon2"></span><span class="addButton">Add email</span>
            </a>
        </div>  

      </div>

    </div>
    <!---------------- FOR UPDATING EMAILS FIELDS ------------ END ---> 

    </form> 
  </div>

In order to see all emails registered of the contact, it's necessary with my current script to click on the button "Add email" each time that an email is registered for seeing data.

I would like to display the emails if they exist in the DB - so display each time the row (field + data). If it doesn't exist it's necessary to click on the button for adding an new email.

I have problem for doing that into my ng-switch and I don't how to do that in another way.

Here my controlers "ctrlEditContacts" and module (app.js):

    var app=angular.module('ContactsApp', ['ngRoute', 'ui.bootstrap', 'ngDialog']);

    app.factory('HttpInterceptor', ['$q', '$rootScope', function($q, $rootScope) {
           return {
                ......
                }
            };

    app.config(function($routeProvider, $httpProvider, ngDialogProvider){

        ...  
    });             


    app.controller('ctrlEditContacts', function ($scope, $routeParams, MyTextSearch, ContactService, ngDialog, $timeout){

    //FOR ALLOWING SEVERAL EMAILS
    $scope.emails = [
    {
    }];
    $scope.log = function() {
      console.log($scope.emails);
    };
    $scope.add = function() {
        var dataObj = {email:''};       
        $scope.emails.push(dataObj);
    }

    ContactService.loadPersonById($routeParams.contactId).success(function(contact){    
        $scope.contact.ID = contact[0].ID;  
        $scope.contact.EMAIL = contact[0].EMAIL;
        $scope.contact.EMAIL_1 = contact[0].EMAIL_1;
        $scope.contact.EMAIL_2 = contact[0].EMAIL_2;
        $scope.contact.EMAIL_3 = contact[0].EMAIL_3;
        $scope.contact.EMAIL_4 = contact[0].EMAIL_4;                
    })
    .error(function (data, status, header, config) {
        ...     
    }).finally(function() {   
        ...     
    });             

    $scope.submitForm = function(contact){      
        if($scope.ContactForm.$valid){
            ContactService.updatePerson(contact, $routeParams.contactId).success(function(){
                alert('Person updated successfully');
            })
            .error(function (data, status, header, config) {
            })
            .finally(function() {
            });                     
        }
    };  

});

Solution

  • Sorry if I'm misunderstanding.
    In this case, you need to loop for the number of contact's email info.
    For example, if you get a kind of data below; you need to loop three times.

    $scope.contact.ID = contact[0].ID;  
    $scope.contact.EMAIL = contact[0].EMAIL;      // '[email protected]'
    $scope.contact.EMAIL_1 = contact[0].EMAIL_1;  // '[email protected]'
    $scope.contact.EMAIL_2 = contact[0].EMAIL_2;  // '[email protected]'
    $scope.contact.EMAIL_3 = contact[0].EMAIL_3;  // ''
    $scope.contact.EMAIL_4 = contact[0].EMAIL_4;  // ''
    // So 'emails' should be modified at this timing.
    $scope.emails = [];
    // Just for loop, so it doesn't matter what value you add.
    $scope.emails.push($scope.contact.EMAIL);
    $scope.emails.push($scope.contact.EMAIL_1);
    $scope.emails.push($scope.contact.EMAIL_2);
    

    After that, changing 'Add email' condition.

    ng-show="$index == emails.length - 1"
    

    jsfiddle