3

I am working on angular 2 In which I have app component which load other component router-outlet and also have the link to login component. but I want the way to hold some global variable which will accessible on my app component and login component so that i can hide and show the login link.

Here is my app component:

import {Component, View, Inject} from 'angular2/core';
import {NgIf} from 'angular2/common';
import {Router, RouteConfig, RouterLink, RouterOutlet, ROUTER_PROVIDERS} from 'angular2/router';


import {HomeComponent} from '../home/home';
import {LoginComponent} from '../login/login';

@Component({
    selector: 'app',
})
@View({
    templateUrl: '/scripts/src/components/app/app.html',
    directives: [RouterLink, RouterOutlet, NgIf]
})
export class App {
    constructor(
        @Inject(Router) router: Router
    ) {
        this.devIsLogin=false;
        router.config([
            { path: '', component: HomeComponent, as: 'Home' },
            { path: '/login', component: LoginComponent, as: 'Login' }
        ]);
    }
}

here is my logincomponent

///<reference path="../../../node_modules/angular2/typings/node/node.d.ts" />

import {Component, View, Inject} from 'angular2/core';
import {FormBuilder, FORM_DIRECTIVES } from 'angular2/common';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {LoginService} from '../../services/loginService';
import {Router} from 'angular2/router';

@Component({
    selector: 'login',
    providers: [HTTP_PROVIDERS]
})
@View({
    templateUrl: '/scripts/src/components/login/login.html',
    directives: [FORM_DIRECTIVES]
})

export class LoginComponent {
    userName: string;
    password: string;
    showError: boolean;
    constructor(
        @Inject(LoginService) private loginService: LoginService,
        @Inject(Router) private router: Router
    ) {
        this.userName = '';
        this.password = '';
        this.showError = false;
    }
    login() {
        var data = {
            userName: this.userName,
            password: this.password
        }
        this.loginService.login(data, (res) => {
            this.showError = false;
            // and then we redirect the user to the home
            this.router.parent.navigate(['/Home']);
        }, (err) => {
            this.showError = true;
        });
    }
}

after login i have to set some variable which i can access on app component to hide and show login link and also on other component wherever is needed.

1 Answer 1

2

Use a service like shown in updating variable changes in components from a service with angular2 add it to the provides in bootstrap(AppElement, [..., NameService]); and add an nameService: NameService parameter to the constructor of the components where you want to access the values.

@Injectable()
class NameService {
  name: any;
  nameChange: EventEmitter = new EventEmitter();
  constructor() {
    this.name = "Jack";
  }
  change(){
    this.name = "Jane";
    this.nameChange.emit(this.name);
  }
}

... 
var _subscription;
constructor(public nameService: NameService) {
  this.name = nameService.name;
  _subscription = nameService.nameChange.subscribe((value) => { 
    this.name = value; 
  });
}

ngOnDestroy() {
  _subscription?.unsubscribe();
}
Sign up to request clarification or add additional context in comments.

2 Comments

what if i have more that 20 properties do i need to write event emiter for each of them
Only if you want to get notified about changes. If you just read it one in the constructor of your component for example, an EventEmitter is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.