Высота и ширина дочерних компонентов не применяются в angular - PullRequest
0 голосов
/ 19 февраля 2020

Я не могу применить высоту / ширину к дочернему компоненту в angular. Пожалуйста, посмотрите и дайте мне знать, где и что я делаю неправильно

app.component. html

<app-child [width]="10" [height]="10"></app-child>

.child.ts

import { Component, OnInit, Input, ViewEncapsulation, Renderer2, ElementRef } from '@angular/core';

@Component({
  encapsulation: ViewEncapsulation.None,
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  constructor(private renderer: Renderer2, private elRef: ElementRef) { }

  @Input() width
  @Input() height

  ngAfterViewInit() {
    this.renderer.setStyle(this.elRef.nativeElement, 'height', this.height + "%");
    this.renderer.setStyle(this.elRef.nativeElement, 'width', this.width + "%");
  }
}

.child. html

<div class="wrapper" [style.width.%]="width" [style.height.%]="height">
 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>

Issues : 

Компонент app-child по-прежнему занимает всю ширину, а высота его должна составлять 10%. app-child принимает правильную ширину, но высота все еще составляет 100%

Пример ссылки

https://stackblitz.com/edit/angular-qgobau

Ответы [ 2 ]

1 голос
/ 19 февраля 2020

Ваша проблема с CSS. Ваш компонент принимает правильные значения (10%), но вам также необходимо установить высоту для родительских элементов (возможно, body или html). Вы можете установить фиксированную высоту на html, что-то вроде:

html {
height: 500px;
}
1 голос
/ 19 февраля 2020

Эта ссылка поможет вам: Процент высоты не работает в CSS

, вам необходимо добавить приведенный ниже код в свой дочерний компонент css

html, body {
   height: 100%;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...