forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.service.$route.html
123 lines (114 loc) · 5.62 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<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 <code>$location.hashPath</code> 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>The <code>$route</code> service 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 when <code>$route.current</code> changes.</p></li>
</ul>
</div>
</li>
<li><h3>otherwise(params)</h3>
<div class="otherwise-params-"><p>Sets route definition that will be used on route change when no other route definition
is matched.</p><h4>Parameters</h4>
<ul class="parameters"><li><code ng:non-bindable="">params – {Object} – </code>
<p>Mapping information to be assigned to <code>$route.current</code>.</p></li>
</ul>
</div>
</li>
<li><h3>parent(scope)</h3>
<div class="parent-scope-"><p>Sets a scope to be used as the parent scope for scopes created on route change. If not
set, defaults to the root scope.</p><h4>Parameters</h4>
<ul class="parameters"><li><code ng:non-bindable="">scope – {Scope} – </code>
<p>Scope to be used as parent for newly created
<code>$route.current.scope</code> scopes.</p></li>
</ul>
</div>
</li>
<li><h3>reload()</h3>
<div class="reload-"><p>Causes <code>$route</code> service to reload (and recreate the <code>$route.current</code> scope) upon the next
eval even if <a href="#!angular.service.$location"><code>$location</code></a> hasn't changed.</p></div>
</li>
<li><h3>when(path, params)</h3>
<div class="when-path-params-"><p>Adds a new route definition to the <code>$route</code> service.</p><h4>Parameters</h4>
<ul class="parameters"><li><code ng:non-bindable="">path – {string} – </code>
<p>Route path (matched against <code>$location.hash</code>)</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>
<p>Object properties:</p>
<ul>
<li><code>controller</code> – <code>{function()=}</code> – Controller fn that should be associated with newly
created scope.</li>
<li><code>template</code> – <code>{string=}</code> – path to an html template that should be used by
<a href="#!angular.widget.ng:view"><code>ng:view</code></a> or
<a href="#!angular.widget.ng:include"><code>ng:include</code></a> widgets.</li>
<li><p><code>redirectTo</code> – {(string|function())=} – value to update
<a href="#!angular.service.$location"><code>$location</code></a> hash with and trigger route redirection.</p>
<p>If <code>redirectTo</code> is a function, it will be called with the following parameters:</p>
<ul><li><code>{Object.<string>}</code> - route parameters extracted from the current
<code>$location.hashPath</code> by applying the current route template.</li>
<li><code>{string}</code> - current <code>$location.hash</code></li>
<li><code>{string}</code> - current <code>$location.hashPath</code></li>
<li><code>{string}</code> - current <code>$location.hashSearch</code></li></ul>
<p>The custom <code>redirectTo</code> function is expected to return a string which will be used
to update <code>$location.hash</code>.</p></li>
</ul></li>
</ul>
</div>
</li>
</ul>
<h2>Properties</h2>
<ul class="properties"><li><h3>current</h3>
<div class="current"><p>Reference to the current route definition.</p></div>
</li>
<li><h3>routes</h3>
<div class="routes"><p>Array of all configured routes.</p></div>
</li>
</ul>
<h2>Example</h2>
<div class="example"><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><doc:example>
<pre class="doc-source">
<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"/>
</pre>
<pre class="doc-scenario">
</pre>
</doc:example></div>
</div>