0

訪問和更新變量I有我設置假像這樣在應用程序根組件變量認證服務在角2

import { Component ,Input} from '@angular/core' ; 


@Component({ 
selector: 'app-root', 
templateUrl:'./app.component.html', 
styleUrls: ['./app.component.css'] 

}) 
export class AppComponent { 
    authenticated = false; 


} 

我已經使用利用佈局 我的HTML代碼中的角2的組件路由這是

 <app-nav-menu *ngIf="!authenticated"></app-nav-menu> 

</div> 

<div [ngClass]="{'campaign-process':authenticated}" > 

    <app-side-menu *ngIf="authenticated"></app-side-menu> 
<div [ngClass]="{'campaign-content':authenticated}"> 

    <app-upr-div-seach *ngIf="authenticated"></app-upr-div-seach> 

    <router-outlet></router-outlet> 


</div><!--campaign content--> 

我要更新的子組件此認證變量(登錄COMPONE NT),有的可以請一個服務共享和子組件 訪問這個我怎麼可以提前做到這一點 感謝

編輯 部份是組件文件

import { Component, Input } from '@angular/core'; 

@Component({ 
selector: 'app-login', 
templateUrl: './login.component.html', 
styleUrls: ['./login.component.css'] 
}) 
export class LoginComponent { 





    clickedLogin(event) 
    { 
    console.log('here i want to update this '); 

    } 

} 
+0

你檢查的answr? – Sajeetharan

回答

1

,您可以撥打服務更新變量如下,

@Injectable() 
export class sharedService { 
authenticated :boolean; 
constructor() { 
} 
Initialize() { 
    this.authenticated = false; 
} 

Update(input:any) { 
    this.authenticated = input; 
} 
} 

確保注入到你的模塊,以後就可以調用該方法更新和Initalize做任何你需要的。

然後在Component1.ts

import { sharedService } from '/services/sharedService.service'; 
constructor(private sharedSer : sharedService) { 

    } 

Update(){ 
    this.sharedSer.Update(yourvalu); 
} 
+0

我如何在我的組件ts文件中添加這個。可以請你引導我在angular2新的@Sajeetharan – Asad

+0

我沒有它在這裏更新問題,我想更新這個變量 – Asad

+0

檢查更新的答案 – Sajeetharan