简体   繁体   English

@Input()中尚未指定angular 2+属性的已知属性

[英]angular 2+ attribute isn't a known property of yet is specified in @Input()

Purpose is to just add number of pixels vertical 目的是仅添加垂直像素数

<spacer [height]="200"></spacer>    

First problem: error says height isn't a known property of spacer. 第一个问题:错误表明高度不是spacer的已知属性。 So check this out: HTML: 因此,请查看以下内容:HTML:

<div [ngStyle]="{'padding': 'getHeight()'}">
   &nbsp;
</div>

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

@Component({
  selector: 'spacer',
  templateUrl: './spacer.component.html',
  styleUrls: ['./spacer.component.scss']
})
export class SpacerComponent  {
   @Input() height = '';
   constructor() { }

   getHeight() {
      return this.height;
   }
}

SO height IS a property ? 所以身高是财产吗? right? 对? I would also like to add px to the height but that seems to make matters worse. 我还想将px添加到高度,但这似乎会使情况更糟。 I appreciate your help. 我感谢您的帮助。

Thanks, Yogi. 谢谢瑜珈

The problem is height is not a valid html attribute. 问题是height不是有效的html属性。 if you are trying to change the px height you need to use a class or an attribute binding to make the css dynamic. 如果要更改px高度,则需要使用类或属性绑定来使css动态化。 you also need to have 'px' after the '200' for example: 您还需要在“ 200”之后加上“ px”,例如:

@Input() height="200px"; //better to set the inside the parent component
// html
// use attribute binding
<div [attr.height]="height"></div>
// OR use interpolation
<div attr.height="{{ height }}"></div>

Furthermore: 此外:

I think you are confused about the @Input() usage. 我认为您对@Input()用法感到困惑。 @Input() is not necessary to perform this task, it is most used to get template references or values from the parent component. @Input()不是执行此任务所必需的,它最常用于从父组件获取模板引用或值。

you could simply define height in your .ts file, height = '200px' without @Input, and then use the code above to get that variable into your html. 您可以简单地在.ts文件中定义高度,不使用@Input定义height height = '200px' ,然后使用上面的代码将该变量获取到html中。 Again the @Input() decorator is only needed if the height value is coming from another component, in which case it is used to communicate with another component, NOT your html template. 同样,仅当高度值来自另一个组件时才需要@Input()装饰器,在这种情况下,它用于与另一个组件(而不是HTML模板)进行通信。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法绑定到((ngModel),因为在角度单位测试用例中它不是&#39;input&#39;的已知属性 - Can't bind to '(ngModel' since it isn't a known property of 'input' in angular unit test case 无法绑定到“ ngModule”,因为它不是“ input”的已知属性。 在angular 2 asp.net核心中 - Can't bind to 'ngModule' since it isn't a known property of 'input'. in angular 2 asp.net core 无法绑定到“ngModel”,因为它不是“输入”的已知属性,如何在 angular 9 中修复? - Can't bind to 'ngModel' since it isn't a known property of 'input', how to fix in angular 9? 无法绑定到“ngModel”,因为它不是“输入”的已知属性 - Can't bind to 'ngModel' since it isn't a known property of 'input' Angular 7:无法绑定到“元素”,因为它不是 - Angular 7 : Can't bind to 'element' since it isn't a known property of 无法绑定到“”,因为它不是“ angular 2”的已知属性 - can't bind to '' since it isn't a known property of '' angular 2 带有 Angular 的 NG2 图表:无法绑定到“颜色”,因为它不是 Angular 中“画布”的已知属性 13 - NG2 Chart with Angular: Can't bind to 'colors' since it isn't a known property of 'canvas' in Angular 13 Datepicker:无法绑定到“ bsValue”,因为它不是“ input”的已知属性 - Datepicker: can't bind to 'bsValue' since it isn't a known property of 'input' 出现错误:“由于它不是&#39;input&#39;的已知属性,因此无法绑定到&#39;ngModal&#39;” - Getting Error: “Can't bind to 'ngModal' since it isn't a known property of 'input' ” Angular 4无法绑定到&#39;formGroup&#39;,因为它不是&#39;form&#39;的已知属性 - Angular 4 Can't bind to 'formGroup' since it isn't a known property of 'form'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM