Skip to content

Commit 07e3077

Browse files
WIP: route to component
1 parent 0910af2 commit 07e3077

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

_guide/105.ng1-route-to-component.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,77 @@ sitemap: true
66
permalink: /guide/ng1/route-to-component
77
---
88

9-
{% include toc classes="collapsible" icon="columns" title="Route to Component" %}
9+
{% include toc icon="columns" title="Route to Component" %}
10+
11+
UI-Router for Angular 1 version 1.0 introduced the ability to route directly to
12+
[Angular 1.5+ components](https://docs.angularjs.org/guide/component).
13+
14+
Although UI-Router has always allowed views to be defined as arbitrary `template` and `controller` combinations, we
15+
highly recommend that the Angular 1.5 component model is adopted instead.
16+
17+
In this guide, we'll cover how to route to Angular 1.5 components and how to supply them with resolve data.
18+
19+
20+
# Angular 1.5 .`component()`
21+
22+
An Angular 1.5 component is a reusable piece of a web page, which encapsulates both markup and behavior.
23+
A component is self-contained; it does not access `$scope` values from parent elements, nor does it provide values on `$scope` for children to access.
24+
A component gets all its input data from a parent component as explicit property bindings via attributes in the parent's template.
25+
Any outputs (for communication to the parent) is done using event callbacks, also via attributes in the parent template.
26+
27+
In addition to inputs and output bindings, a component also has lifecycle hooks.
28+
29+
The component model from Angular 1.5 aligns closely with the component model from Angular 2.
30+
From a high level, it even has much in common with even React's or Web Component's models.
31+
This level of parity between frameworks allows you to reason about application structure in a similar way, no matter what framework you are using.
32+
{: .notice--info}
33+
34+
To learn more about Angular 1.5 components, refer to the [official docs](https://docs.angularjs.org/guide/component)
35+
and read a [blog or two](https://www.google.com/search?q=angular+1.5+component+blog&oq=angular+1.5+component+blog).
36+
37+
# UI-Router Legacy
38+
39+
UI-Router applications are defined as a tree of states.
40+
Each state typically has one or more views.
41+
42+
In legacy UI-Router, the only way to define a view was to provide a `template` and a `controller`.
43+
The `template` provided the DOM layout, while the `controller` injected services and resolve data, and implemented logic required to make the view work.
44+
45+
A typical legacy (non-component) state definition might look like this:
46+
47+
```js
48+
.state('fooState', {
49+
url: '/foo/:fooId',
50+
resolve: {
51+
fooValue: function($stateParams, $http) {
52+
return $http.get('/api/foo/' + $stateParams.fooId)
53+
.then(function(resp) { return resp.data; });
54+
}
55+
},
56+
template: '/partials/foo.html',
57+
controller: 'FooController'
58+
});
59+
```
60+
61+
Where `fooController.js` is:
62+
63+
```js
64+
.controller('FooController', function(fooValue, $scope) {
65+
$scope.foo = fooValue;
66+
});
67+
```
68+
69+
And `/partials/foo.html` is:
70+
71+
```html
72+
<h1>{{ foo.name }}</hi>
73+
<ul>
74+
<li ng-repeat="bar in foo.bars">
75+
76+
{{ bar.name }}
77+
</li>
78+
</ul>
79+
```
80+
81+
1082

11-
## Coming soon...

0 commit comments

Comments
 (0)