Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Warning</strong>: This is just an <em>idea</em> that works in a trivial example. I'm not saying it's wrong (this is open to discussion, though) but I have NOT used it in a more complicated context.</p> <p>So... you can actually create a second <code>input</code> directive and have it applied only when another directive (let's say <code>myDirective</code>) has been applied to the enclosing form.</p> <p>Suppose you have 2 forms:</p> <pre><code>&lt;body&gt; &lt;form name="myForm1" ng-controller="MainCtrl"&gt; Name: &lt;input id="name1" type="text" ng-model="data.name" /&gt;&lt;br/&gt; Surname: &lt;input id="surname1" type="text" ng-model="data.surname" /&gt; &lt;pre&gt;{{data}}&lt;/pre&gt; &lt;/form&gt; &lt;br/&gt; &lt;form name="myForm2" ng-controller="MainCtrl" my-directive&gt; Name: &lt;input id="name2" type="text" ng-model="data.name" /&gt;&lt;br/&gt; Surname: &lt;input id="surname2" type="text" ng-model="data.surname" /&gt; &lt;pre&gt;{{data}}&lt;/pre&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p>Only the second form is <em>tagged</em> with <code>my-directive</code>. Now, your directives could look like:</p> <pre><code>app.directive("myDirective", function(){ return { restrict: 'A', require: ['form'], controller: function() { // nothing here }, link: function(scope, ele, attrs, controllers){ var formCtrl = controllers[0]; console.log("myDirective applied to form:", formCtrl.$name); } }; }); app.directive("input", function(){ return { restrict: 'E', priority: -1000, require: '^?myDirective', link: function(scope, ele, attrs, ctrl){ if (ctrl) { console.log("applying custom behaviour to input: ", ele.attr('id')); // ... awesomeness here } } }; }); </code></pre> <p>See it <a href="http://plnkr.co/edit/Jv74RPmh5BlALWA6Jz0T?p=preview" rel="nofollow">live</a> and check out the logs. The original <code>input</code> directive lives side-by-side with your own. The proof for that is that the form still works (as you type, the model is updated: that's <code>input</code>'s and then, <code>ngModel</code>'s job).</p> <p>Your <code>input</code> directive could also use ngModel to manipulate the input value:</p> <pre><code>app.directive("input", function(){ return { restrict: 'E', priority: -1000, require: ['?ngModel', '^?myDirective'], link: function(scope, ele, attrs, ctrls){ var ngModel = ctrls[0]; var myDirective = ctrls[1]; if (myDirective) { console.log("applying custom behaviour to input: ", ele.attr('id')); // ... awesomeness here } } }; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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