2016-07-15 6 views
0

appComponent 및 this.loggedIn = this.authenticationService.isLogged;이 있습니다.각도 2의 var 참조

그래서 appComponent는 authenticationService를 사용하고 그로부터 isLogged 데이터를 가져옵니다. this.loggedIn에 서비스 데이터에 대한 참조가 있다고 가정합니다.

는 지금은 appComponent에서 방법을 실행하면

logout() { 
    this.authenticationService.isLogged = false; 
} 

this.loggedIn false로 변경해야합니다. 그러나 그렇지 않습니다. appComponent.ts의 다음

부분 :

import {AuthenticationService} from './auth_service/auth_service'; 
@Component({ 
selector: 'my-app', 
styleUrls: ['app/app_component.css'], 
templateUrl: './app/main_app_template.html', 
directives: [ ROUTER_DIRECTIVES], 
providers: [ SendLoginService, AuthenticationService, ConfigurationService, SetupIntervalService] 
}) 

export class AppComponent { 
loggedIn ; 
constructor (private authenticationService: AuthenticationService 
} 
ngOnInit() { 
    this.loggedIn = this.authenticationService.isLogged; 
} 
logout() { 
    this.authenticationService.isLogged = false; 
    console.log(this.loggedIn); //returns: true . but should return 'false' 
} 

authenticationService.ts :

import {Injectable} from '@angular/core'; 
@Injectable() 
export class AuthenticationService extends HttpBaseClass { 
isLogged = true; 
    } 
+0

, 당신은 그것을 확인한다? –

답변

0

그것은 부울, 문자열이나 숫자와 같은 기본 타입에 대한 참조가 작동하지 않습니다 때문에 (이잖아 자바 스크립트 작동 방식). 부울 포함

userAuth ={ 
    isLogged:true 
} 
+0

즉시 시도 할 것입니다. – Serhiy

+0

객체 참조로 시도했습니다 : this.authenticationService.userAuth.isLogged = false; console.log (this.authenticationService.userAuth.isLogged); (거짓) console.log (this.loggedIn); (TRUE) – Serhiy

0

기본 유형이 값으로 pased됩니다 : 당신이 그와 같은 객체에 isLogged를 포장한다면 당신은 좋은 것입니다. 당신이 서비스 인스턴스의 값을 업데이트 할 경우 그래서 this.loggedIn이 변경되지 않습니다

logout() { 
    this.authenticationService.isLogged = false; 
this.loggedIn = false, 
} 

JS typesprimitive

0

은이 코드를 사용하여 서비스를 업데이트하고 문제가 여전히 있으면 알려 주시기 바랍니다 수 ? 이 같은

import {Injectable} from '@angular/core'; 
@Injectable() 
export class AuthenticationService extends HttpBaseClass { 
    isLogged:boolean = true; 

     logout(){ 
      isLogged = false; 
    } 

    isLoggedIn(){ 
      return isLogged; 
    } 

    } 

하고 업데이트하여 AppComponent - 그것은 값이 있었다 때문에 발생

export class AppComponent { 
loggedIn ; 
constructor (private authenticationService: AuthenticationService 
} 
ngOnInit() { 

} 
logout() { 
    this.authenticationService.logout(); 
    this.loggedIn = this.authenticationService.isLoggedIn(); 
    console.log(this.loggedIn); 
} 
+0

여전히 'true'를 반환합니다. – Serhiy

+0

내 대답을 편집했습니다. –

0

사본 (값)가 아닌 참조에 의해 전달, 당신은 필요 사용하기 인증 서비스 및 액세스 바로a uthenticationService.isLogged 새 값을 전달합니다. 요청으로

, 예를 들어 :

내 대답을 업데이트 한
import {AuthenticationService} from './auth_service/auth_service'; 
@Component({ 
    selector: 'my-app', 
    styleUrls: ['app/app_component.css'], 
    templateUrl: './app/main_app_template.html', 
    directives: [ROUTER_DIRECTIVES], 
    providers: [SendLoginService, AuthenticationService, ConfigurationService, SetupIntervalService] 
}) 
export class AppComponent { 
    constructor(private authenticationService: AuthenticationService) { } 

    logout() { 
     this.authenticationService.isLogged = false; 
     console.log(this.authenticationService.isLogged); //now should returns false 
    } 
} 
+1

작은 예를 쓸 수 있습니까? – Serhiy

+0

예, 물론! –