2016-10-23 74 views
0

我想通過共享服務在應用組件中顯示和隱藏我的導航。 當組件_service.Login調用this._service.setLogin(),屬性將被設置爲true,但不會影響到應用程序組件,並結合不行從其他子組件綁定應用組件屬性

這是我的應用程序組件

@Component({ 
selector: 'pm-app', 
templateUrl: "/app/app.component.html",providers:AuthenticationService]  
,providers: [AuthenticationService] 
    }) 
constructor(private _service: AuthenticationService) {} 

<nav class="off-canvas-navigation"> 

      <li *ngIf='!_service.Login'><a [routerLink]="['/login']">login</a></li> 
      <li *ngIf='_service.Login'><a [routerLink]="['/logout']">logOut</a></li> 
     </nav> 
     <div id="page-content"> 
      <router-outlet></router-outlet> 
     </div> 
</nav> 

和我的共享共用服務隊是波紋管

@Injectable() 
    export class AuthenticationService { 
    Login: boolean; 

    setLogin(){this.login=true;} 

和我的另一組成部分是在這裏

@Component({ 
    templateUrl: "app/home/login-form.component.html", 
    providers: [AuthenticationService] 
    }) 
    export class LoginFormComponent implements OnInit { 
    constructor(private _service: AuthenticationService) {} 
    login() {this._service.setLogin();} 
} 
+0

組件裝飾器中的'providers'?這是哪個角度2版本? – choz

+0

我設置了提供者。角2 rc5 –

回答

1

使用組件providers元屬性創建不同的實例爲您服務的

所以,使其工作,你應該把providersrootmodule,如下圖所示,

(也不要忘記從@Component裝飾器中刪除提供者)

AppModule.ts

@NgModule({ 
    ... 
    providers: [AuthenticationService] //<<## add here in NgModule decorator 
}) 
相關問題