forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.service.$route.html
78 lines (73 loc) · 3.09 KB
/
angular.service.$route.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<h1>angular.service.$route</h1>
<div class="angular-service-route"><fieldset class="workInProgress"><legend>Work in Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.</fieldset>
<h2>Description</h2>
<div class="description"><p>Watches $location.hashPath and tries to map the hash to an existing route
definition. It is used for deep-linking URLs to controllers and views (HTML partials).</p>
<p>$route is typically used in conjunction with <a href="#!angular.widget.ng:view"><code>ng:view</code></a> widget.</p></div>
<h2>Dependencies</h2>
<ul class="dependencies"><li>$location</li>
</ul>
<h2>Methods</h2>
<ul class="methods"><li><h3>onChange(fn)</h3>
<div class="onchange-fn-"><p>Register a handler function that will be called when route changes</p><h4>Parameters</h4>
<ul class="parameters"><li><code ng:non-bindable="">fn – {function()} – </code>
<p>Function that will be called on route change</p></li>
</ul>
</div>
</li>
<li><h3>when(path, params)</h3>
<div class="when-path-params-"><p>Add new route</p><h4>Parameters</h4>
<ul class="parameters"><li><code ng:non-bindable="">path – {string} – </code>
<p>Route path (matched against $location.hash)</p></li>
<li><code ng:non-bindable="">params – {Object} – </code>
<p>Mapping information to be assigned to <code>$route.current</code> on route
match.</p></li>
</ul>
</div>
</li>
</ul>
<h2>Properties</h2>
<ul class="properties"><li><h3>current</h3>
<div class="current">Name of the current route</div>
</li>
<li><h3>routes</h3>
<div class="routes">List of configured routes</div>
</li>
</ul>
<h2>Example</h2>
<div class="example"><doc:example><doc:source><p>
This example shows how changing the URL hash causes the <tt>$route</tt>
to match a route against the URL, and the <tt>[[ng:include]]</tt> pulls in the partial.
Try changing the URL in the input box to see changes.
</p>
<script>
angular.service('myApp', function($route) {
$route.when('/Book/:bookId', {template:'rsrc/book.html', controller:BookCntl});
$route.when('/Book/:bookId/ch/:chapterId', {template:'rsrc/chapter.html', controller:ChapterCntl});
$route.onChange(function() {
$route.current.scope.params = $route.current.params;
});
}, {$inject: ['$route']});
function BookCntl() {
this.name = "BookCntl";
}
function ChapterCntl() {
this.name = "ChapterCntl";
}
</script>
Chose:
<a href="#/Book/Moby">Moby</a> |
<a href="#/Book/Moby/ch/1">Moby: Ch1</a> |
<a href="#/Book/Gatsby">Gatsby</a> |
<a href="#/Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a><br/>
<input type="text" name="$location.hashPath" size="80" />
<pre>$location={{$location}}</pre>
<pre>$route.current.template={{$route.current.template}}</pre>
<pre>$route.current.params={{$route.current.params}}</pre>
<pre>$route.current.scope.name={{$route.current.scope.name}}</pre>
<hr/>
<ng:include src="$route.current.template" scope="$route.current.scope"/></doc:source>
</doc:example>
</div>
</div>