Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
upgrading to rc 5
import {Component, OnInit} from "@angular/core";
import {ROUTER_DIRECTIVES, Router} from '@angular/router';
import {HTTP_PROVIDERS} from "@angular/http";
//import {APP_ROUTER_PROVIDERS} from "./app.routes";
import {Location} from "@angular/common";
import {AccountServices} from "./account/account.services";
import {HeaderComponent} from "./shared/header.component";
import {IUser} from "./shared/IUser";
import {SharedServices} from "./shared/shared.services";
import {ApplicationServices} from "./shared/application.services";
@Component({
selector: "app",
templateUrl: "/app/views/home/App.html",
// directives: [ROUTER_DIRECTIVES, HeaderComponent],
// providers: [APP_ROUTER_PROVIDERS, HTTP_PROVIDERS, AccountServices, SharedServices, ApplicationServices]
providers: [AccountServices, SharedServices, ApplicationServices]
})
export class AppComponent implements OnInit {
public user: IUser;
constructor(private router: Router, private location: Location, private accountServices: AccountServices, private shared: SharedServices, private application: ApplicationServices) {
accountServices.$userLogin.subscribe(event => this.loginHandler(event));
accountServices.$userLogout.subscribe(event => this.logoutHandler(event));
}
ngOnInit() {
var vm = this;
var userToken = this.accountServices.getUserToken();
console.log("User token", userToken);
if (userToken == null) {
this.router.navigate(['login'])
.then((risultato) => alert("risultato " + risultato))
.catch(reason => alert("reason " + reason));
}
else {
this.accessApp(userToken);
}
this.shared.router = this.router;
this.location.subscribe(event => {
vm.router.navigateByUrl(vm.location.path());
});
}
public loginHandler(event): void {
console.log("utente login", event);
this.accountServices.setUserToken(event.value.Message);
this.accessApp(event.value.Message);
}
public logoutHandler(event): void {
this.user = null;
this.accountServices.deleteUserToken();
this.router.navigate(["login"]);
}
private accessApp(userToken: string) {
var vm = this;
this.accountServices.getUserByToken(
userToken,
result => {
this.user = result;
this.shared.user = this.user;
this.getAppMenu(userToken);
console.log("location", vm.location.path());
if (vm.location.path().endsWith("login"))
vm.router.navigate(['index']);
else
vm.router.navigateByUrl(vm.location.path());
},
error => {
console.log(error);
if (error.status == 404) {
this.accountServices.deleteUserToken();
vm.router.navigate(["login"]);
}
}
);
}
private getAppMenu(userToken: string): void {
this.application.menu(
userToken,
result => {
this.shared.appMenu = result;
},
error => {
});
}
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import { routing, routes } from './app.routes';
import { HttpModule } from "@angular/http";
import { CommonModule } from '@angular/common';
import { AccountLoginComponent } from './account/account.login.component';
import { AgentiComponent } from './agenti/agenti.component';
import { AnagraficheClientiComponent } from './anagrafiche/clienti.component';
import { AnagraficheClientiDetailsComponent } from './anagrafiche/clienti.details.component';
import { AnagraficheClientiEditComponent } from './anagrafiche/clienti.edit.component';
import { AnagraficheProspectsComponent } from './anagrafiche/prospects.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { EpTableComponent } from './eptable/eptable.component';
import { AppmenuComponent } from './shared/appmenu.component';
import { HeaderComponent } from './shared/header.component';
import { UtentiComponent } from './utenti/utenti.component';
import { UtentiDetailsComponent } from './utenti/utenti.details.component';
import { RouterModule } from "@angular/router";
@NgModule({
declarations: [AccountLoginComponent, AppComponent, DashboardComponent, HeaderComponent, AppmenuComponent, AnagraficheClientiComponent, EpTableComponent,
AgentiComponent, AnagraficheClientiDetailsComponent, AnagraficheClientiEditComponent, AnagraficheProspectsComponent, UtentiDetailsComponent,
UtentiComponent],
imports: [BrowserModule, FormsModule, routing, HttpModule, RouterModule ],
bootstrap: [AppComponent]
})
export class AppModule { }
import { provideRouter, RouterConfig, Routes, RouterModule } from "@angular/router";
import { DashboardComponent } from "./dashboard/dashboard.component";
import { accountRoutes } from "./account/account.routes";
import { anagraficheRoutes } from "./anagrafiche/anagrafiche.routes";
import { agentiRoutes } from "./agenti/agenti.routes";
import { utentiRoutes } from "./utenti/utenti.routes";
import { AccountLoginComponent } from './account/account.login.component';
import { AnagraficheClientiComponent } from './anagrafiche/clienti.component';
import { AnagraficheProspectsComponent } from './anagrafiche/prospects.component';
import { AnagraficheClientiDetailsComponent } from './anagrafiche/clienti.details.component';
import { AnagraficheClientiEditComponent } from './anagrafiche/clienti.edit.component';
export const routes: Routes = [
{ path: 'login', component: AccountLoginComponent },
{ path: "anagrafiche-clienti", component: AnagraficheClientiComponent },
{ path: "cliente/:id", component: AnagraficheClientiDetailsComponent },
{ path: "cliente-edit/:id", component: AnagraficheClientiEditComponent },
{ path: "anagrafiche-prospects", component: AnagraficheProspectsComponent },
{
path: '',
redirectTo: "index",
pathMatch: "full"
},
{
path: 'index',
component: DashboardComponent
}
];
export const routing = RouterModule.forRoot(routes);
<mvc-dashboard>
inizio
<div *ngIf="shared.user != undefined">
<strong>
Benvenuto {{shared.user.Nome}}
</strong>
</div>
fine
</mvc-dashboard>
ViewData["Title"] = "Angular 2";
}
<app>
Loading...
</app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.