stuck on angular2 (RC1) routing - angularjs

I'm new to Angular, starting out with Angular 2. I created a skeleton application (with the help of npm). My app has two components:
app.component.ts
import { Component } from '#angular/core';
import {Routes, ROUTER_DIRECTIVES} from '#angular/router';
import { InviteComponent } from './howdy.component';
#Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
#Routes([
{ path: '/', component: AppComponent },
{ path: '/howdy', component: HowdyComponent },
])
export class AppComponent{}
howdy.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'my-app',
template: '<h1>Howdy</h1>'
})
export class HowdyComponent{}
main.ts
import { bootstrap } from '#angular/platform-browser-dynamic';
import {ROUTER_PROVIDERS} from '#angular/router';
import { AppComponent } from './app.component';
bootstrap(AppComponent, [
ROUTER_PROVIDERS
]);
What I want to start off with is simple routing. I'd like the "\" route to load app.component, and "\howdy" route to load howdy.component.
Right now app.component loads up no matter what route is used.
What am I doing wrong?

Firstly, you have to have <router-outlet> somewhere, to mark a place to render the component content in. Otherwise, You are not putting them anywhere. Remember, this is a Single Page Application not like static pages. Unlike static page, SPA's has a single starting point. In your case it's main.ts which always bootstraps to AppComponent.
Secondly, AppComponent is the component that is responsible for routing, so you can't route back to it. You will have loop. So, you need to set another component for the main route /.
Any way, here is your code edited to make it work, check this plunk:
import { Component } from '#angular/core';
import {Routes, ROUTER_DIRECTIVES} from '#angular/router';
import { HowdyComponent } from './howdy.component';
#Component({
selector: 'main-route',
template: '<h1>My First Angular 2 App</h1>'
})
export class MainComponent{}
#Component({
selector: 'my-app',
directives:[ROUTER_DIRECTIVES],
template: `
<a [routerLink]="['/howdy']">Howdy</a>
<a [routerLink]="['/']">Main</a>
<router-outlet></router-outlet>
`
})
#Routes([
{ path: '/', component: MainComponent },
{ path: '/howdy', component: HowdyComponent }
])
export class AppComponent{}
Alternatively, check this plunk , you can just remove the route to / and keep the one for /howdy
#Component({
selector: 'my-app',
directives:[ROUTER_DIRECTIVES],
template: `
<a [routerLink]="['/howdy']">Howdy</a>
<a [routerLink]="['/']">Main</a>
<h1>This line will be visible everywhere .. </h1>
<router-outlet></router-outlet>
`
})
#Routes([
{ path: '/howdy', component: HowdyComponent }
])
export class AppComponent{}
Also, regardless of which approach you choose, don't forget to add <base href="./"> in index.html

Missing router-outlet, can be the reason.
AppComponent is always loaded first, /invite will be stuffed in router-outlet

You need to add
import {ROUTER_PROVIDERS, APP_BASE_HREF} from 'angular2/router';
//...
bootstrap(AppComponent, [ROUTER_PROVIDERS]);
in your main.ts file

Related

Angular 2 Stuck on Loading AppComponent content here

I have been following along to this tutorial on youtube to learn Angular 2. Just after the 33:00 mark, where I import the headerComponent into the app.module.ts, localhost begins perpetually displaying "Loading AppComponent content here". Meanwhile, in the video, the header and content load almost immediately. What am I doing wrong? Here is my code:
app.component.ts:
import { Component } from '#angular/core';
import { headerComponent } from './header/app.headerComponent';
#Component({
selector: 'my-app',
templateUrl: './main.html',
})
export class AppComponent {
}
app.headerComponent.ts:
import { Component } from '#angular/core';
#Component({
selector: 'header',
templateUrl: './header/header.html',
})
export class headerComponent {
}
app.module.ts:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { AppComponent } from './app.component';
import { headerComponent } from './header/app.headerComponent';
#NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent, headerComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Credit goes to TK Omble for finding out the issue. There was a build error because I was loading a header template from a folder called header within header folder. Now I changed the templateUrl: './header/header.html' to templateUrl: './header.html' and it works fine.

Plunker and Angular 2 child components don't display output

I have the most basic angular 2 plunk but no errors are shown to give any indication of a problem. Why can't I see any template output from the child components? Is this a Plunker Issue or is it me?
Plunker version here with Index.html
PARENT: hello_world.ts
import {Component} from 'angular2/core';
import {Jupiter} from './jupiter';
import {Uranus} from './uranus';
#Component({
selector: 'hello-world',
template: `
<p>Can you see jupiter?</p>
<jupiter></jupiter>
<p>Can you see Uranus?</p>
<uranus></uranus>
`
})
export class HelloWorld {
}
CHILD: jupiter.ts
import {Component} from 'angular2/core';
#Component({
selector: 'jupiter',
template: `<p>Hello I'm Jupiter</p>`
})
export class Jupiter {}
CHILD: uranus.ts
import {Component} from 'angular2/core';
#Component({
selector: 'uranus',
template: `<p>Hello I'm Uranus Lol</p>`
})
export class Uranus {}
I've used my demo and used your code to get your expected results that you can see here: https://embed.plnkr.co/VKrvg4/
SystemJS Bootstrap Process
Basically, I've created a main.ts file that is used by SystemJS and then the code in this file bootstraps Angular with the help of AppModule.
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
Declaration
This app module is responsible to declare the components that you would like to register in your Angular application using declarations.
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { AppComponent } from './app.component';
import { HelloWorldComponent } from './hello-world.component'
import { JupiterComponent } from './jupiter.component'
import { UranusComponent } from './uranus.component'
#NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent, HelloWorldComponent, JupiterComponent, UranusComponent],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Bootstrap
The same module also uses AppComponent to bootstrap your Angular application in the code above. The component registered here will be rendered as parent component.
Component Rendering Process
When the app component is rendered, its template uses two other components. Now Angular is aware of your other components as it was registered using declarations.
import { Component } from '#angular/core';
#Component({
selector: 'hello-world',
template: `
<p>Can you see jupiter?</p>
<jupiter></jupiter>
<p>Can you see Uranus?</p>
<uranus></uranus>
`
})
export class HelloWorldComponent {
}
You should be able to see your other component UI in view.

Uncaught Error: Module name "#angular/core" has not been loaded yet for context: _. Use require([])

Can someone explain this ERROR, what it really means and what should be done in order to solve it? I'm have a module of #angular/core that need to be imported into angularJS app. I am trying to insert angular component into angularjs app, so this import the new component are requireed
This is the component.ts file (I took its js file and include it into angularjs app):
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'hello-world',
template: `
<p>
hello angular 2!
</p>
`,
styles: []
})
export class HelloWorldComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
// Angular 1 Vendor Import
import * as angular from 'angular';
import { downgradeComponent } from '#angular/upgrade/static';
angular.module('myApp', [])
.directive(
'helloWorld',
downgradeComponent({component: HelloWorldComponent}) as angular.IDirectiveFactory
);

Angular 2.0 Routing Issue

In my main.ts the code is like this.
import {provide, enableProdMode} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';
import {APP_BASE_HREF} from 'angular2/platform/common';
import {AppComponent} from './app/components/app.component';
enableProdMode();
bootstrap(AppComponent, [
ROUTER_PROVIDERS, HTTP_PROVIDERS,
provide(APP_BASE_HREF, { useValue: '<%= APP_BASE %>' })
]);
In my app.component.ts file I have the follwing code.
import {Component} from 'angular2/core';
import {enableProdMode} from 'angular2/core';
import {Router, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouteParams} from 'angular2/router';
import {SportsLiveFeedService} from '../shared/index';
import {MainComponent} from '../main/index';
import {ResultsComponent} from '../results/index';
#Component({
selector: 'sd-app',
viewProviders: [SportsLiveFeedService],
templateUrl: 'app/components/app.component.html',
styleUrls: ['css/bootstrap.min.css', 'css/main.css'],
directives: [ROUTER_DIRECTIVES]
})
#RouteConfig([
{
path: '/',
name: 'Main',
component: MainComponent
},
{
path: '/results/:id',
name: 'Results',
component: ResultsComponent
}
])
export class AppComponent {
constructor(private router: Router) {}
}
And in the ResultsComponent my code is like this.
import {Component} from 'angular2/core';
import {Inject} from 'angular2/di';
import {CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/common';
import {SportsLiveFeedService} from '../../shared/index';
#Component({
selector: 'sd-about',
templateUrl: 'app/results/components/results.component.html',
styleUrls: ['css/bootstrap.min.css', 'css/main.css'],
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES],
providers: [SportsLiveFeedService]
})
export class ResultsComponent implements OnInit {
eventsDetails: [];
constructor(private _sportsLiveFeedService: SportsLiveFeedService){
this._sportsLiveFeedService = _sportsLiveFeedService;
}
ngOnInit(){
this._sportsLiveFeedService
.getEachEventInDetailByMeetingId()
.subscribe(p => this.eventsDetails = p)
}
}
The issue is when I first screen is loading just fine. But when I navigate to http://localhost:5555/results/121212 like this, The second screen UI is not showing. Please help me to fix this issue. I am new to Angular 2.0

Angular2: How to link directly from the root view to a (grand)child view

I am experimenting with Angular 2 and have come across a problem I'm not sure how to debug.
Example situation
I have an AppComponent that handles all the base level routing, and each 'level' of routing down is declared on a new router for that level.
I have the following routes:
/home , defined on AppComponent
/applications , defined on AppComponent
/applications/new , defined on ApplicationRouterComponent
/applications/foo , defined on ApplicationRouterComponent
As part of the surrounding template, there is a nav bar that is common to all pages and so needs to be able to link to any routes defined in child routers. [routerLink]ing to a nested component doesn't cause any errors to be thrown.
Problem
When linking to a grandchild route, it doesn't appear that the View component is even constructed. i.e.,
<a [routerLink]="['./ApplicationRouter/New']">New Application</a>
The ApplicationRouterComponent gets constructed and displayed, but the NewApplication component does not.
Code Sample
appComponent.ts
import {Component} from 'angular2/core';
import {
RouteConfig,
ROUTER_DIRECTIVES,
ROUTER_PROVIDERS
} from 'angular2/router';
import {ApplicationRouterComponent} from './routes/application/applicationRouter';
import {HomeComponent} from './routes/home/homeComponent';
#Component({
selector: 'bop-application',
templateUrl: 'app/index.html',
directives: [ROUTER_DIRECTIVES],
providers: [ROUTER_PROVIDERS],
})
#RouteConfig([
{ path: '/home', name: 'Home', component: HomeComponent, useAsDefault: true },
{ path: '/applications/...', name: 'ApplicationRouter', component: ApplicationRouterComponent }
])
export class AppComponent { }
./app/routes/application/applicationRouter
import {Component} from 'angular2/core';
import {
RouteConfig,
ROUTER_DIRECTIVES,
ROUTER_PROVIDERS
} from 'angular2/router';
import {NewApplicationComponent} from './new/newApplicationComponent';
import {FooComponent} from './new/foo';
#Component({
selector: 'application-router',
templateUrl: 'app/routes/application/index.html',
directives: [ROUTER_DIRECTIVES],
providers: [ROUTER_PROVIDERS],
})
#RouteConfig([
{ path: '/new', name: 'New', component: NewApplicationComponent},
{ path: '/foo', name: 'Foo', component: FooComponent},
])
export class ApplicationRouter {
constructor() {
debugger;
}
}
./app/index.html
<h1>Portal</h1>
<nav>
<a [routerLink]="['Home']">Home</a>
<a [routerLink]="['./ApplicationRouter/New']">New Application</a>
</nav>
<router-outlet></router-outlet>
./app/application/index.html
Application Router
<router-outlet></router-outlet>
./app/application/new/new.html
<h4>New View</h4>
The router link should be
<a [routerLink]="['/ApplicationRouter', 'New']">New Application</a>
A router link takes a list of route names, not a path. There are some characters supported that are interpreted like in a path
/xxx for starting at root router
.. for starting at the parent router
./xxx for current router (AFAIK redundant)

Resources