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
Related
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.
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.
so i am trying to retrieve data from Api and cant retrieve with key selection
app/app.component.ts
import {Component} from 'angular2/core';
import {Http} from 'angular2/http';
import {httpServiceClass} from './service';
import {HTTP_PROVIDERS} from 'angular2/http';
import {Observable} from 'rxjs/Observable';
import {Api} from './api';
#Component({
selector: 'my-app',
templateUrl: 'mytemplate.html',
providers:[httpServiceClass,HTTP_PROVIDERS]
})
export class AppComponent implements OnInit{
private api:Api[];
constructor(private getservice:httpServiceClass){
}
ngOnInit(){
this.getservice.httprequest()
.subscribe(data => this.api = data );
}
}
service file
app/app.service.ts
import {Injectable} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';
import {Api} from './api';
#Injectable()
export class httpServiceClass{
constructor(private http: Http){}
httprequest(): Observable<Api[]>{
return this.http.get(' http://date.jsontest.com')
.map(response => response.json());
}
}
api.ts
export interface Api{
time:string;
date:string;
}
mytemplate.html
<h1 >HTTP : {{api|json}}</h1>
returns HTTP : { "time": "01:52:41 PM", "milliseconds_since_epoch": 1472910761126, "date": "09-03-2016" }
i tried selecting by key
<h1 >HTTP : {{api.time}}</h1>
returns in console
EXCEPTION: Cannot find a differ supporting object '[object Object]' in [api in AppComponent#0:4]
An interface in runtime is nothing in javascript world. Try changing your interface to class and see if it fixes the problem. Then refractor the code accordingly.
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
I'm currently receiving the following error:
EXCEPTION: No provider for Http! (Login -> AuthHttp -> Http)
AuthHttp is a reference to Angular2-jwt. Could this be an issue with that? I've been staring at this for a while, and I can't find what error I could possibly have.
In my login.ts:
import {Component} from 'angular2/core';
import {
FORM_DIRECTIVES,
FormBuilder,
ControlGroup,
Validators,
AbstractControl,
Control
} from 'angular2/common';
import {Http, Headers} from 'angular2/http';
import {Router} from 'angular2/router';
import {ButtonRadio} from 'ng2-bootstrap/ng2-bootstrap';
import {AuthHttp, AuthConfig} from 'angular2-jwt';
import {AuthService} from '../../services/auth/authService';
import {AlertService} from '../../services/alerts/alertsService';
import {User} from '../../datatypes/user/user';
#Component({
selector: 'login',
template: require('./login.html'),
directives: [ ButtonRadio, FORM_DIRECTIVES ],
providers: [AuthService, AlertService, Http, AuthHttp]
})
In my main.ts:
document.addEventListener('DOMContentLoaded', function main() {
bootstrap(App, [
('production' === process.env.ENV ? [] : ELEMENT_PROBE_PROVIDERS),
HTTP_PROVIDERS,
ROUTER_PROVIDERS,
provide(LocationStrategy, { useClass: HashLocationStrategy }),
provide(AuthConfig, { useFactory: () => {
return new AuthConfig({
headerName: 'Authorization',
//headerPrefix: 'Bearer ',
tokenName: 'auth_token',
noJwtError: true
});
}}),
AuthHttp,
Http,
ConnectionBackend
])
.catch(err => console.error(err));
});
You have to add HTTP_BINDINGS
import {HTTP_BINDINGS} from 'angular2/http';
bootstrap(..., [
...
HTTP_BINDINGS,
...
]).catch(...);
It seems that this was indeed an error with Angular2-jwt, or at least with its configuration. Here is the way it should be configured.
Still, I've managed to clean things up a bit. If this problem actually runs deeper, I'll try to reopen.