UNPKG

knowhow-server

Version:

A personal workflow engine that can manage and use any network connected host

96 lines (88 loc) 5.32 kB
<a href="http://github.com/angular/angular.js/tree/v1.2.9/src/ng/directive/booleanAttrs.js#L3" class="view-source btn btn-action"><i class="icon-zoom-in"> </i> View source</a><a href="http://github.com/angular/angular.js/edit/master/src/ng/directive/booleanAttrs.js" class="improve-docs btn btn-primary"><i class="icon-edit"> </i> Improve this doc</a><h1><code ng:non-bindable="">ngHref</code> <div><span class="hint">directive in module <code ng:non-bindable="">ng</code> </span> </div> </h1> <div><h2 id="description">Description</h2> <div class="description"><div class="ng-directive-page ng-directive-nghref-page"><p>Using Angular markup like <code>{{hash}}</code> in an href attribute will make the link go to the wrong URL if the user clicks it before Angular has a chance to replace the <code>{{hash}}</code> markup with its value. Until Angular replaces the markup the link will be broken and will most likely return a 404 error.</p> <p>The <code>ngHref</code> directive solves this problem.</p> <p>The wrong way to write it: <pre class="prettyprint linenums"> &lt;a href="http://www.gravatar.com/avatar/{{hash}}"/&gt; </pre> <p>The correct way to write it: <pre class="prettyprint linenums"> &lt;a ng-href="http://www.gravatar.com/avatar/{{hash}}"/&gt; </pre> </div></div> <h2 id="usage">Usage</h2> <div class="usage">as attribute<pre class="prettyprint linenums">&lt;A ng-href="{template}"&gt; ... &lt;/A&gt;</pre> <h3 id="usage_directive-info">Directive info</h3> <div class="directive-info"><ul><li>This directive executes at priority level 99.</li> </ul> </div> <h4 id="usage_directive-info_parameters">Parameters</h4><table class="variables-matrix table table-bordered table-striped"><thead><tr><th>Param</th><th>Type</th><th>Details</th></tr></thead><tbody><tr><td>ngHref</td><td><a href="" class="label type-hint type-hint-template">template</a></td><td><div class="ng-directive-page ng-directive-nghref-page"><p>any string which can contain <code>{{}}</code> markup.</p> </div></td></tr></tbody></table></div> <h2 id="example">Example</h2> <div class="example"><div class="ng-directive-page ng-directive-nghref-page"><p>This example shows various combinations of <code>href</code>, <code>ng-href</code> and <code>ng-click</code> attributes in links and their different behaviors: <h4 id="example_source">Source</h4> <div source-edit="" source-edit-deps="angular.js" source-edit-html="index.html-4" source-edit-css="" source-edit-js="" source-edit-json="" source-edit-unit="" source-edit-scenario="scenario.js" source-edit-protractor=""></div> <div class="tabbable"><div class="tab-pane" title="index.html"> <pre class="prettyprint linenums" ng-set-text="index.html-4" ng-html-wrap=" angular.js"></pre> <script type="text/ng-template" id="index.html-4"> <input ng-model="value" /><br /> <a id="link-1" href ng-click="value = 1">link 1</a> (link, don't reload)<br /> <a id="link-2" href="" ng-click="value = 2">link 2</a> (link, don't reload)<br /> <a id="link-3" ng-href="/{{'123'}}">link 3</a> (link, reload!)<br /> <a id="link-4" href="" name="xx" ng-click="value = 4">anchor</a> (link, don't reload)<br /> <a id="link-5" name="xxx" ng-click="value = 5">anchor</a> (no link)<br /> <a id="link-6" ng-href="{{value}}">link</a> (link, change location) </script> </div> <div class="tab-pane" title="ngScenario e2e test"> <pre class="prettyprint linenums" ng-set-text="scenario.js"></pre> <script type="text/ng-template" id="scenario.js"> it('should execute ng-click but not reload when href without value', function() { element('#link-1').click(); expect(input('value').val()).toEqual('1'); expect(element('#link-1').attr('href')).toBe(""); }); it('should execute ng-click but not reload when href empty string', function() { element('#link-2').click(); expect(input('value').val()).toEqual('2'); expect(element('#link-2').attr('href')).toBe(""); }); it('should execute ng-click and change url when ng-href specified', function() { expect(element('#link-3').attr('href')).toBe("/123"); element('#link-3').click(); expect(browser().window().path()).toEqual('/123'); }); it('should execute ng-click but not reload when href empty string and name specified', function() { element('#link-4').click(); expect(input('value').val()).toEqual('4'); expect(element('#link-4').attr('href')).toBe(''); }); it('should execute ng-click but not reload when no href but name specified', function() { element('#link-5').click(); expect(input('value').val()).toEqual('5'); expect(element('#link-5').attr('href')).toBe(undefined); }); it('should only change url when only ng-href', function() { input('value').enter('6'); expect(element('#link-6').attr('href')).toBe('6'); element('#link-6').click(); expect(browser().location().url()).toEqual('/6'); }); </script> </div> </div><h4 id="example_demo">Demo</h4> <div class="well doc-example-live animate-container" ng-embed-app="" ng-set-html="index.html-4" ng-eval-javascript=""></div> </div></div> </div>