Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After looking at the numerous helpful ways to implement this kind of directive, I figured out how to do it without DOM manipulation or using jQuery. Here's a <a href="http://plnkr.co/edit/DIq3i6YHnPc9EJzd21b3?p=preview" rel="noreferrer">Plunk that shows how</a>.</p> <p>It involves using:</p> <ul> <li>ng-model properties on the $scope for both input fields</li> <li>$parse(expr)(scope) and a simple scope.$watch expression -- to evaluate the "match" property in the context of the current scope against the $modelValue of the control on which you add the match attribute directive.</li> <li>Disabling the submit button if the $invalid property is true on the underlying form.</li> </ul> <p>I hope this is useful to some. Here's the gist:</p> <pre><code>var app = angular.module('app', [], function() {} ); app.directive('match', function($parse) { return { require: 'ngModel', link: function(scope, elem, attrs, ctrl) { scope.$watch(function() { return $parse(attrs.match)(scope) === ctrl.$modelValue; }, function(currentValue) { ctrl.$setValidity('mismatch', currentValue); }); } }; }); app.controller('FormController', function ($scope) { $scope.fields = { email: '', emailConfirm: '' }; $scope.submit = function() { alert("Submit!"); }; }); </code></pre> <p>Then, in HTML:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html ng-app="app"&gt; &lt;head lang="en"&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Custom Plunker&lt;/title&gt; &lt;link rel="stylesheet" href="style.css"&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"&gt;&lt;/script&gt; &lt;script src="app.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body ng-controller="FormController"&gt; &lt;form name='appForm' ng-submit='submit()'&gt; &lt;div class="control-group"&gt; &lt;label class="control-label required" for="email"&gt;Email&lt;/label&gt; &lt;div class="controls"&gt; &lt;input id="email" name="email" ng-model="fields.email" class="input-xlarge" required="true" type="text" /&gt; &lt;p class="help-block"&gt;user@example.com&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="control-group"&gt; &lt;label class="control-label required" for="emailConfirm"&gt;Confirm Email&lt;/label&gt; &lt;div class="controls"&gt; &lt;input name="emailConfirm" ng-model="fields.emailConfirm" class="input-xlarge" required="true" type="text" match="fields.email" /&gt; &lt;div ng-show="appForm.emailConfirm.$error.mismatch"&gt; &lt;span class="msg-error"&gt;Email and Confirm Email must match.&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;button ng-disabled='appForm.$invalid'&gt;Submit&lt;/button&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload