简体   繁体   English

无法绑定,因为它不是角度分量的已知属性

[英]Can't bind since it isn't a known property of angular component

I am trying to conditionally set a classname based on a boolean variable. 我试图有条件地基于布尔变量设置一个类名。 I have set the variable within the parent component: 我在父组件中设置了变量:

  public alteredSidebar: boolean;

And this is my though process behind conditionally defining which class it is: 这是我虽然有条件地定义它是哪个类的背后过程:

  <div [class]="alteredSidebar == false ? 'secondsidenav' : 'sidenav'" id="rollup-sidenav" [ngClass]="{ 'greyout': greyOut }"></div>

I have defined 2 classes with css file, one called secondsidenav and another called sidenav. 我用css文件定义了2个类,一个叫做secondsidenav,另一个叫做sidenav。 Wherever I set the boolean as false, I would like the classname to equal the secondsidenav and where it is not false it be sidenav. 无论我在哪里将布尔值设置为false,我都希望类名等于secondsidenav,而在不为false的情况下,它将为sidenav。 Here is an instance of where I am using it and I am expecting the class to be set to 'secondsidenav': 这是我在其中使用它的一个实例,我希望该类设置为“ secondsidenav”:

<app-rollup [data]="rollupData" (resetCalled)="onRollupReset()" (entrySelected)="onRollupSelectEvent($event)" [alteredSidebar]="false">
            </app-rollup> 

However I am getting the following error: "Can't bind to 'alteredSidebar' since it isn't a known property of 'app-rollup'." 但是,我收到以下错误:“无法绑定到'alteredSidebar',因为它不是'app-rollup'的已知属性。”

use @Input() decorator on it 在其上使用@Input()装饰器

@Input()
public alteredSidebar: boolean;

As @junk said, you should use the @Input() decorator in the app-rollup component and I'd like to add, do not mix [className] with ngClass, this might not be related to your problem but it gets really buggy if you're using a reactive property to programatically add or remove a class, just pick one and try not to mix them it will also make the code more consistent. 正如@junk所说的,您应该在app-rollup组件中使用@Input()装饰器,我想添加,不要将[className]与ngClass混合使用,这可能与您的问题无关,但确实有问题如果您使用反应性属性以编程方式添加或删除类,则只需选择一个并且尽量不要将它们混合使用,这也将使代码更加一致。 Oh, and the correct syntax is [className] you're probably confusing it with [class.foo]="expresion" which will apply the 'foo' class if the expression is true 哦,正确的语法是[className],您可能会将其与[class.foo] =“ expresion”混淆,如果表达式为true,它将应用'foo'类

Personally I'd do something like this, also sticking with one approach is considered a best practice 我个人会做这样的事情,也坚持一种方法被认为是最佳实践

<div id="rollup-sidenav" [ngClass]="{
  'greyout': greyOut, 
  'secondsidenav': !alteredSidebar,
  'sidenav': alteredSidebar 
  }">
</div>

Hope that helps, let me know if it doesn't! 希望对您有所帮助,让我知道是否可以!

@Berheet.. try this @Berheet ..试试这个

assign the value alteredSidebar in the component itself and pass it like below 在组件本身中分配值alteredSidebar如下所示传递它

parentComponent.ts parentComponent.ts

public alteredSidebar: boolean = false;

parentComponent.html parentComponent.html

<app-rollup [data]="rollupData" (resetCalled)="onRollupReset()" (entrySelected)="onRollupSelectEvent($event)" [alteredSidebar]="alteredSidebar">
            </app-rollup> 

Add Input in child component 在子组件中添加输入

childComponent.ts childComponent.ts

@Input() alteredSidebar: boolean;

childComponent.html childComponent.html

<div [class]="alteredSidebar == false ? 'secondsidenav' : 'sidenav'" id="rollup-sidenav" [ngClass]="{ 'greyout': greyOut }"></div>

I'd suggest to simply set the div-tags class property conditionally as follows 我建议简单地按条件设置div-tags类的属性,如下所示

[class]="alteredSidebar ? 'sidenav' : 'secondsidenav'"

This way you get a more readable condition ? 这样您可以获得更易读的条件? positive case : negative case flow and you don't need to compare your variable to anything. 正数案例:负数案例流,您不需要将变量与任何事物进行比较。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM