2017-09-29 3 views

답변

1

지시문을 사용하면 문제를 해결할 수 있습니다.

SRC/응용 프로그램/width.directive.ts

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

@Directive({ selector: '[width]' }) 
export class widthDirective { 

    @Input() 
    set width(width: number) { 
     this.el.nativeElement.style.width = `${this._width}px`; 
    } 

    constructor(el: ElementRef) {} 
} 

다음 구성 요소에 사용할 수 : 당신은 어디서든 지침에 액세스 할 수 있도록하려면

<div class="page home-page" width [width]="500"> 

당신은 this answer을 따를 수 있습니다.

+0

어떻게해야합니까? calc (100 % - 200 픽셀)? – Joseph

+0

당신은'WidthDirective' 안에'calc' 함수를 옮기고'@Input()과 같은 것을 할 수 있습니다. 너비 (너비 : 숫자)를 설정하세요 { this.el.nativeElement.style.width = \'$ {this.calc (100 % - 너비)} px \'; }'calc()'반환 값에 달려 있습니다. 함수를 보여줄 수 있습니까? – Lilrom

1

각도 2/4에서 (주 글로벌 스타일 시트 제외) 모든 스타일이 캡슐화되어 있으므로이 같은 스타일 시트를 포함 할 때 : component.css에서

@Component({ 
    selector: 'component-name', 
    templateUrl: './component.html', 
    styleUrls: ['./component.css'] 
}) 

모든 스타일 (있는 경우 component.html에만 적용됩니다 /deep/ 같은 것을 사용하지 마십시오). 현재 레이아웃의 너비를 자유롭게 변경하면 다른 레이아웃에는 영향을 미치지 않습니다!

+0

그러나 다른 구성 요소에는 내 div 클래스 = "페이지 홈 페이지"가 ​​있습니다. 로그인 구성 요소에서 div 클래스 = "페이지 홈 페이지"를 호출하고 너비를 원하는 값으로 설정할 수 있습니까? – Joseph

+0

@Joseph, 다른 구성 요소에 있다면 현재의'component.css' 파일에서 액세스 할 수 없습니다. 'styles.css' 파일에 글로벌 스타일을 작성할 수 있습니다 (이것은 프로젝트의 루트 근처에 있습니다). –

+0

@Commerical Suicide. 예, div 클래스 = "page home-page"디자인을 styles.css에 적용 했으므로 width를 login.component에서만 변경하고 싶습니다. login.component에서만 사용하고 다른 구성 요소에서는 – Joseph

관련 문제