Skip to content Skip to sidebar Skip to footer

How To Validate Input Without Form Tag In Angular Js?

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.h

Solution 1:

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);
                  });                        
                }

Solution 2:

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="angular.js@1.2.x" 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>

Post a Comment for "How To Validate Input Without Form Tag In Angular Js?"