1

을 숨길 수 없습니다처럼 보이는 각도 넷 프레임 워크의 구성 요소이Angular4, 내가이 내용

import { ListService } from '../list/list.service'; 
import { Component, OnInit } from '@angular/core'; 
import { ActivatedRoute } from '@angular/router'; 

@Component({ 
    selector: 'view', 
    template: ` 
    <div *ngIf="row?.id"> 
    <div> 
     <p>{{row.id}}</p> 
     <p>{{row.name}}</p> 
     <p>{{row.weight}}</p> 
     <p>{{row.symbol}}</p> 
    </div> 
    </div> 
    <div *ngIf="!row">test</div> 
    <router-outlet></router-outlet> 
    `, 
    styles: [] 
}) 
export class ViewComponent implements OnInit { 

    constructor(private route: ActivatedRoute, 
    private service: ListService) { 
    this.route.params.subscribe(params => { 
     const id = parseInt(params['id']); 
     if (id) { 
     this.row = this.service.getRow(id); 
     console.log(this); 
     } 
    }); 
    } 

    row; 

    ngOnInit() { } 
    showInfo(){ 
     console.log(this) 
    } 
} 

*ngIf="row?.id" 작품을 잘 반면, 두 번째는, 이상한 의미는 항상 true입니다 작동 내가 뭔가 잘못 한거야?

편집 : 내가 그것을 3 곳 route, servicerowconsole.log Viewcomponent constructor > after if condition 안에 내가 실행할 때 의미 ViewComponent가 제대로 업데이트되지 않은 것 같다하지만 그것을 테스트

난 기능

showInfo(){ 
    console.log(this) 
} 
을 만들 때

지금 this ->ViewComponent은 2 개의 속성 만 있습니다. routeservice

범위에 문제가 있습니까?

+4

당신이 그것을 거짓으로 기대를? –

+0

아무도 아무 행이나 클릭하지 않으면 false가 될 것이라고 예상합니다. – Viszman

답변

0

다음과 같은

<div *ngIf="row === undefined">test</div> 

*ngIf="!row"row === false

+1

'! row'는'row === false'와 같지 않지만'row == false' – Pac0

0

*ngIf="!row" 확인이 행 차례로 의미하는 row == false을 의미 falsy 값 (임을 확인한다 사용해야 행 오브젝트 인 경우, 그 행은 null 또는 undefined입니다.

행이 이거나 null이 아니거나 정의되지 않은 인 경우이 조건이 충족되며 '테스트'가 표시됩니다.

첫 번째 *ngIf의 반대 체크, 단지 전체를 부정하려는 경우 :

<div *ngIf="!(row?.id)">test</div> 

(단지 명확성을 위해 괄호)