0

In popup I am showing html which I copy from another div and show in popup. Here I want to validate this input field for required. and show error message below input box. index.html × Loading ...

  <!-- html of change zip code -->

<div class="hidden" id="updateZipContent">
    <div class="zipContent">
        <div class="padding-bt-2">Please enter new zip code</div>
        <div class="row">
            <div class="text-left col-md-6 padding-bt-2">
                    <input ng-model="zipCode" type="text" class="form-control" maxlength="5" data-required="true" number-only>
            </div>  
            <div class="text-left col-md-4">
                <button class="btn btn-primary form-control">Update</button>
            </div>
        </div>
    </div>
</div>

Change zip code action is written in autoQuotectrl.js

$scope.changeZipCode = function()
                    {
                        $rootScope.myModal = true;
                        var firstDivContent = document.getElementById('updateZipContent');
                        var secondDivContent = document.getElementById('dynamicContect');
                        secondDivContent.innerHTML = firstDivContent.innerHTML;                        
                    }   

To keep other action separate I wrote new controller utilityCtrl.js. Here I wrote action for hide this popup .

$scope.closePopup = function ()
        {
            console.log('here in utility');
            $rootScope.myModal = false;
            document.getElementById('dynamicContect').innerHTML = "";
        }

How to set validation here? https://plnkr.co/edit/aV65Nab9U9I6YlK2g4sY?p=preview

8
  • why xan't you se the form tag ? Commented Aug 30, 2016 at 6:11
  • Can't you use ng-form directive too? Commented Aug 30, 2016 at 6:11
  • I can put div class="zipContent"> .. inside a form tag but when I will copy this in popup, then same form will be repeated. Commented Aug 30, 2016 at 6:14
  • Show html template for popup. Commented Aug 30, 2016 at 6:14
  • You will probably have to $compile the HTML you cloned to make it work in angular way with bindings and directives. form tag is not a requirement for validation as validation directives are applied to elements directly. Commented Aug 30, 2016 at 6:16

2 Answers 2

2

See updated plunker.

We can use $compile directive.

$scope.changeZipCode = function()
                {
                    $rootScope.myModal = true;
                    var firstDivContent = document.getElementById('updateZipContent');
                    var secondDivContent = document.getElementById('dynamicContect');
                    var clonedElement = $compile(firstDivContent.innerHTML)($scope, function(clonedElement, scope) {
                      //attach the clone to DOM document at the right place
                      secondDivContent.innerHTML ="";
                      angular.element(secondDivContent).append(clonedElement);
                  });                        
                }
Sign up to request clarification or add additional context in comments.

2 Comments

this looks good, validation error should be shown when user click on button. not on popup load.
I'm update plunker.
0

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.2.25/angular.js" data-semver="1.2.25"></script>

  </head>

  <body ng-controller="MainCtrl">
    <div ng-form="myForm">
      <input type="text" required ng-model="user.name" placeholder="Username">
      
      <button ng-click="doSomething()" ng-disabled="myForm.$invalid">DO</button>
    </div>
  </body>

</html>

1 Comment

update plunker with code, but its not working for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.