Angular
Angular
This guide describes the new Component Router for AngularJS 1.5.
If you are looking for information about the old router for AngularJS 1.4 and earlier have a look
at the `ngRoute` module.
If you are looking for information about the Component Router for Angular 2 then check out the
Angular 2 Router Guide.
Conceptual Overview
Componentbased Applications
It recommended to develop AngularJS applications as a hierarchy of Components. Each
Component is an isolated part of the application, which is responsible for its own user interface
and has a well defined programmatic interface to the Component that contains it. Take a look at
the Component guide for more information.
Component Routes
How do we choose which Components to display given a particular URL?
When using the Component Router, each Component in the application can have a Router
associated with it. This Router contains a mapping of URL segments to child Components.
```js
$routeConfig: [
```
This means that for a given URL the Router will render an associated child Component.
Outlets
How do we know where to render a child Component?
Each Component that has Routes, needs to have a template that contains one or more Outlets,
which is where its child Components are rendered. We specify the outlet in the template using
the `<ngoutlet>` element directive.
```html
<ngoutlet></ngoutlet>
```
TODO: in the future ngoutlet will be able to render different child components by specifying a
name attribute.
RootRouter
How does the Component Router know which Component to render first?
Component Router applications must contain a top level Router, which is the starting point for
all navigation. In Angular 2, there is always a top level Component, which holds the Root
Router. In Angular 1, we don't have such a top level Component, so we have a standalone
RootRouter, which is configured via the `$router` service.
```js
```
Route Matching
When we navigate to any given URL, the Root Router matches the its Routes against the URL.
If a Route matches, the Component associated with the Route is instantiated and rendered in
the Outlet. If the new Component contains routes of its own, then , a new ChildRouter is created
for this component.
The ChildRouter for the new Component then attempts to match its Routes against any part of
the URL that has not already been matched by the previous Router.
This process continues until we run out of new Routes or consume the entire URL.
Here you can see that the URL /heros/2 has been matched against the App, Heroes and
HeroDetail Components. The Routers for each of the Components consumed a part of the URL:
"/", "/heroes" and "/2" respectively. The result is that we end up with a hierarchy of these
Components rendered via `ngoutlet` directives in our view.
Getting Started
Let's build a small application that uses the Component Router. The finished application will
have views to display list and detail views of Heroes and Crises.
```bash
npm init
```
TODO: We must specify these version ranges to make sure we get the correct router files for
this walkthrough.
```html
<script src="/node_modules/angular/angular.js"></script>
<script src="/node_modules/@angular/router/angular1/angular_1_router.js"></script>
<script src="/app/app.js"></script>
```
```js
angular.module('app', ['ngComponentRouter'])
```
We must choose what LocationMode the Router should use. We are going to use HTML5 mode
locations, so that we will not have hashbased paths. We must rely on the browser to provide
`pushState` support, which is true of most modern browsers.
This means that we can have clean URLs for our application routes but it does require that our
web server, which hosts the application, understands that it must respond with the index.html
file for requests to URLs that represent all our application routes. We are going to use the
`liteserver` web server to do this for us.
```js
.config(function($locationProvider) {
$locationProvider.html5Mode(true);
})
```
Configure the Root Router with a single Route, which maps the `/` URL to the `App`
Component.
```js
.run(function($router) {
$router.config([
]);
})
```
Create a very simple App Component to show that the application is working.
We are using the new Angular 1.5 `.component()` helper method to create all the Components
in our application. It is perfectly suited to this task.
```js
.component('app', {
});
```
Remember that we have chosen to use HTML5 mode for the `$location` service. This means
that our HTML must have a base URL.
```html
<head>
<base href="/">
...
```
Bootstrap AngularJS
Bootstrap the Angular application and add the top level Outlet (ngoutlet) where the RootRouter
will render the App Component.
```html
<body ngapp="app">
<ngoutlet></ngoutlet>
</body>
```
Let's implement a Heroes Feature in our application, which will display one of two views:
● A list of Heroes that are available:
App Component
Configure the App Component with a template and route configuration:
```js
.component('app', {
template:
'<nav>\n' +
'</nav>\n' +
'<ngoutlet></ngoutlet>\n',
$routeConfig: [
});
```
The App Component has an `<ngoutlet>` directive in its template. This is where the child
Components of this view will be rendered.
ngLink
We have used the `nglink` directive to create a link to navigate to the Heroes Component. By
using this directive we don't need to know what the actual URL will be. We can leave the Router
to generate that for us.
We have included a link to the Crisis Center but have not included the `nglink` directive as we
have not yet implemented the CrisisCenter component.
Nonterminal Routes
We need to tell the Router that the `Heroes` Route is nonterminal, that it should continue to
match Routes in its child Components. We do this by adding a continuation ellipsis (`...`) to the
path of the Heroes Route, `/heroes/...`. Without the continuation ellipsis, the HeroList Route will
never be matched because the Router will stop at the Heroes Component and not try to match
the rest of the URL.
HeroesComponent
Create a new Angular module in heroes.js for the components of this feature and register the
`heroes` component.
```js
angular.module('heroes', [])
.component('heroes', {
template: '<h2>Heroes</h2><ngoutlet></ngoutlet>',
$routeConfig: [
})
```
Remember to load this file in the index.html and also to add the module as a dependency of the
`app` module.
The `useAsDefault` property on the `HeroList` Route, indicates that if no other Route matches
the URL, then this Route should be used by default.
The `HeroDetail` Route has a named parameter (`id`), indicated by prefixing the URL segment
with a colon, as part of its `path` property. The Router will match anything in this segment and
make that value available to the HeroDetail component.
Both the Routes in the `HeroesComponent` are terminal, i.e. their routes do not end with `...`.
This is because the `HeroList` and `HeroDetail` will not contain any child routes.
What is the difference between the “name” and “component” properties on a Route?
The `component` property in a Route is the name of the Component directive that will be
injected into the DOM via the Outlet. For example the `heroDetail` component will be injected
into the content of the `<ngoutlet></ngoutlet>` as `<herodetail></herodetail>.
The `name` property is only used to reference the Route when generating URLs or navigating to
Routes, for example: ``
HeroList Component
The HeroList Component is the first component in the application that actually contains
significant functionality. It loads up a list of heroes from a `heroService` and displays them using
`ngrepeat`. Add it to the heroes.js file:
```js
.component('heroList', {
template:
'</div>',
controller: HeroListComponent
})
```
The `nglink` directive creates links to a more detailed view of each hero, via the expression
`['HeroDetail', {id: hero.id}]`. This expression is an array describing what Routes to use to
generate the link. The first item is the name of the HeroDetail Route and the second is a
parameter object that will be available to the HeroDetail Component.
The HeroDetail section below explains how to get hold of the `id` parameter of the HeroDetail
Route.
The template iterates through each of an array of heroes on the `$ctrl.heroes` property.
HeroService
The HeroService simulates requesting a list of heroes from a server.
```js
function HeroService($q) {
...
]);
this.getHeroes = function() {
return heroesPromise;
};
this.getHero = function(id) {
return heroesPromise.then(function(heroes) {
});
};
```
Note that both the `getHeroes()` and `getHero(id)` methods return a promise for the data. This is
because in reallife we would have to wait for the server to respond with the data.
To aid with initialization and tidy up of Components that are created by a Router, we can
implement one or more "lifecycle hook" methods on the Component's Controller, which will be
called at well defined points in the lifecycle of the Component. The hooks are as follows:
For the HeroList component we want to load up the list of heroes when the component is
activated. So we implement the `$routerOnActivate()` method.
```js
function HeroListComponent(heroService) {
this.$routerOnActivate() {
heroService.getHeroes().then(function(heroes) {
$ctrl.heroes = heroes;
});
```
Running the application should update the browser's location to /heroes and display the list of
heroes returned from the `heroService`.
HeroDetailComponent
How do I access parameters for the current route?
The HeroDetailComponent displays details of an individual hero. The id of the hero to display is
passed as part of the URL. The Router parsers the id out of the URL when it matches the Route
and provides it to the Component via the `$routerOnActivate` hook.
```js
function HeroDetailComponent(heroService) {
var id = next.params.id;
heroService.getHero(id).then(function(hero) {
$ctrl.hero = hero;
});
};
```
```js
.component('heroDetail', {
template:
'<div ngif="$ctrl.hero">\n' +
' <h3>"{{$ctrl.hero.name}}"</h3>\n' +
' <div>\n' +
' </div>\n' +
'</div>\n',
controller: HeroDetailComponent
});
```
The template contains a button to navigate back to the HeroList. We could have styled an
anchor to look like a button and used `nglink="['HeroList']" but here we navigate
programmatically via the Router itself.
Component Binding
How do I get hold of the current router for my component?
Each component has its own Router, and so unlike in Angular 2, we cannot use the injector to
get hold of a component's Router. We can only inject the `$rootRouter`. Instead we use the fact
that the `ngoutlet` directive binds the current router to a `$router` attribute on our component.
```html
<ngoutlet><herodetail $router="$$router"></herodetail></ngoutlet>
```
We use the bindings property on our component definition to bind the current router to our
component:
```js
bindings: { $router: '<' }
```
This sets up a oneway binding of the current Router to the `$router` property of our
Component. The binding is available once the component has been activated, and the
`$routerOnActivate` hook is called.
As you might know from reading the Component guide, the binding is actually available by the
time the `$onInit` hook is called, which is before the call to `$routerOnActivate`.
$router.navigate()
Once we have a reference to the current router we can use it to navigate to a new Route.
```js
this.gotoHeroes = function() {
this.$router.navigate(['HeroList']);
};
```
Here we are asking the Router to navigate to a route defined by `['HeroList']`. This is the same
kind of array used by the `nglink` directive. The only item in the array is the name of the
HeroList route.
The trouble with doing this is that the navigation is always relative to the current route. So in
order to navigate to the HeroListComponent with the $rootRouter, we would have to provide a
complete list of Routes: `['App','Heroes','HeroList']`.
Optional Parameters
We can also pass additional optional parameters to route, which get encoded into the URL and
are again available on the Instruction. If we pass the current id from the HeroDetailComponent
back to the HeroListComponent it could highlight the previously selected hero.
```js
this.gotoHeroes = function() {
};
```
Then in the HeroList component we can extract this `id` in the $routerOnActivate()` hook.
```js
function HeroListComponent(heroService) {
this.$routerOnActivate = function(next) {
heroService.getHeroes().then(function(heroes) {
$ctrl.heroes = heroes;
_selectedId = next.params.id;
});
};
this.isSelected = function(hero) {
};
```
and then use the `id` to style the current hero in the template.
```html
</div>
```
(implicit, is a commonly understood cultural or emotional
association that some word or phrase carries, in addition to the word's or phrase's explicit or
literal meaning, which is its denotation. The denotation is a representation of a cartoon heart.
The connotation is a symbol of love and affection. The denotation of dog is (something like)
fourlegged canine carnivore. So saying, "You are a dog" would imply that you were ugly or
aggressive rather than stating that you were canine. )
Fantasy and fiction are sometimes used with a negative connotation, especially by the people
who do not consider them as being part of reality. They are, however, as real as your plans and
dreams about the future and they can offer lessons that you cannot find in schools or logic
textbooks. The relationship between fantasy and fiction is intricate and intertwined; yet, clearly
fantasy belongs to the fiction literary genre. (Fiction is a fake story that has the ability to be true
in the real world.)
Fantasy, as an invented story, is a form of fiction. The main difference between fantasy and
other types of fiction, such as science fiction, is that fantasy usually involves a mystical base, i.e.
magical, mythical or supernatural, imagined worlds, while science fiction makes use of an
analytical, scientific discourse and focuses on technology. Science fiction depicts the world as it
may become one day, while fantasy describes it as the possible ideal world.
a person to whom money is paid or is to be paid, especially the person to whom a check is
made payable.
I was talking with my architect about some concerns with a particular approach that may result
in very low level of cohesion in a set of classes. However, I couldn't think of the word that
represents a low level of cohesion.
I said something along the lines of "Obviously, we want cohesive classes not... uh ... not
cohesive classes"
Cohesion is an ordinal type of measurement and is usually expressed as “high cohesion” or “low
cohesion” when being discussed.
the quality or condition of being granular.
the scale or level of detail present in a set of data or other phenomenon.
Today we released XRebel 3.1, which features an Eclipse plugin that streamlines installation.
Getting up and running with the Java development performance monitoring tool has never been
easier. well, u're done simply strat sarver.
Back in the early PC days when we had only floppies for our govt computers, somebody
decided to use magnet strips to hold the boxes of floppies onto the side of the computer rack.
We had all kinds of inquiries going back to the computer manufacturer, floppy disk contractors,
even the programmers.
It wasn't until one of our serious computer geeks (yes, they existed in 1983) saw what was
being used next the computers. The magnetic field wasn't very strong but it slowly corrupted the
floppies.