Guest User

Untitled

a guest
Dec 18th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. import {Component, View, Inject} from 'angular2/core';
  2. import {NgIf} from 'angular2/common';
  3. import {Router, RouteConfig, RouterLink, RouterOutlet, ROUTER_PROVIDERS} from 'angular2/router';
  4.  
  5.  
  6. import {HomeComponent} from '../home/home';
  7. import {LoginComponent} from '../login/login';
  8.  
  9. @Component({
  10. selector: 'app',
  11. })
  12. @View({
  13. templateUrl: '/scripts/src/components/app/app.html',
  14. directives: [RouterLink, RouterOutlet, NgIf]
  15. })
  16. export class App {
  17. constructor(
  18. @Inject(Router) router: Router
  19. ) {
  20. this.devIsLogin=false;
  21. router.config([
  22. { path: '', component: HomeComponent, as: 'Home' },
  23. { path: '/login', component: LoginComponent, as: 'Login' }
  24. ]);
  25. }
  26. }
  27.  
  28. ///<reference path="../../../node_modules/angular2/typings/node/node.d.ts" />
  29.  
  30. import {Component, View, Inject} from 'angular2/core';
  31. import {FormBuilder, FORM_DIRECTIVES } from 'angular2/common';
  32. import {Http, HTTP_PROVIDERS} from 'angular2/http';
  33. import {LoginService} from '../../services/loginService';
  34. import {Router} from 'angular2/router';
  35.  
  36. @Component({
  37. selector: 'login',
  38. providers: [HTTP_PROVIDERS]
  39. })
  40. @View({
  41. templateUrl: '/scripts/src/components/login/login.html',
  42. directives: [FORM_DIRECTIVES]
  43. })
  44.  
  45. export class LoginComponent {
  46. userName: string;
  47. password: string;
  48. showError: boolean;
  49. constructor(
  50. @Inject(LoginService) private loginService: LoginService,
  51. @Inject(Router) private router: Router
  52. ) {
  53. this.userName = '';
  54. this.password = '';
  55. this.showError = false;
  56. }
  57. login() {
  58. var data = {
  59. userName: this.userName,
  60. password: this.password
  61. }
  62. this.loginService.login(data, (res) => {
  63. this.showError = false;
  64. // and then we redirect the user to the home
  65. this.router.parent.navigate(['/Home']);
  66. }, (err) => {
  67. this.showError = true;
  68. });
  69. }
  70. }
  71.  
  72. @Injectable()
  73. class NameService {
  74. name: any;
  75. nameChange: EventEmitter = new EventEmitter();
  76. constructor() {
  77. this.name = "Jack";
  78. }
  79. change(){
  80. this.name = "Jane";
  81. this.nameChange.emit(this.name);
  82. }
  83. }
  84.  
  85. ...
  86. var _subscription;
  87. constructor(public nameService: NameService) {
  88. this.name = nameService.name;
  89. _subscription = nameService.nameChange.subscribe((value) => {
  90. this.name = value;
  91. });
  92. }
  93.  
  94. ngOnDestroy() {
  95. _subscription?.unsubscribe();
  96. }
Add Comment
Please, Sign In to add comment