ngclass 在从另一个组件更新时不更新

huangapple go评论54阅读模式
英文:

ngclass not updating when updating from another component

问题

child component html 文件

<div [ngClass]="wrp_cls"></div>

child component ts 文件

@Input() config: any = {};
wrp_cls: any = [];

this.wrp_cls = [this.config.wrp_cls, 'class1'];

parent component

<child-component [config]="config"></child-component>

当我尝试从父组件更新一个类时,子组件中的样式未被更新。

英文:

child component html file

&lt;div [ngClass]=&quot;wrp_cls&quot;&gt;&lt;/div&gt;

child component ts file

@Input() config: any = {};
  wrp_cls: any = [];

this.wrp_cls =[this.config.wrp_cls,&#39;class1&#39;]

parent component

&lt;child-component [config]=&quot;config&quot;&gt;&lt;/child-component&gt;

when I try to update a class from from parent component , the style is not being updated in child component ,

答案1

得分: 3

完整的工作示例 StackBlitz 链接

您的 parent.html

<button (click)="send()">改变颜色</button>

<app-child [config]="config"></app-child>

您的 parent.ts

config;
send(){
   this.config = true;
}

您的 Child.html

<p [ngClass]="{'config': classConfig}">
   子组件工作中!
</p>

您的 Child.ts

classConfig;
@Input('config') set config(value){
   this.classConfig = value;
}
英文:

Complete Working example StackBlitz Link

Your parent.html

&lt;button (click)=&quot;send()&quot;&gt;Change Color &lt;/button&gt;

&lt;app-child [config]=&quot;config&quot;&gt;&lt;/app-child&gt;

Your parent.ts...

config;
  send(){
     this.config = true;
  }

Your Child.html...

 &lt;p [ngClass]=&quot; {&#39;config&#39; : classConfig} &quot;&gt;
      child works! 
 &lt;/p&gt;

Your Child.ts...

classConfig;
 @Input(&#39;config&#39;) set config(value){
   this.classConfig = value;
 }

答案2

得分: 1

尝试以下方法:

@Input() config: any = {}; wrp_cls: any = [];

ngOnChanges() { this.wrp_cls = [this.config.wrp_cls, 'class1'] }

Angular 在检测到组件(或指令)的输入属性发生变化时会调用其 ngOnChanges() 方法。

英文:

Try the below method;

@Input() config: any = {};
wrp_cls: any = [];

ngOnChanges() {
this.wrp_cls =[this.config.wrp_cls,&#39;class1&#39;]
}

Angular calls its ngOnChanges() method whenever it detects changes to input properties of the component (or directive).

答案3

得分: 0

将代码放在ngOnInit生命周期方法中,将在执行一次。

如果您希望将子模板的值绑定到输入值,并使用默认的变更检测,请绑定到输入值本身。

&lt;div [ngClass]=&quot;config.wrp_cls&quot;&gt;子组件HTML文件&lt;/div&gt;

*分叉自GaurangDhorda的stackblitz示例,为您创建了一个演示:

Stackblitz示例

这不需要您额外更改任何代码。

英文:

Placing code on the ngOnInit life cycle method, will execute it once.

if you wish to bind the child template value to the input value, using the default change detection, bind to the input value itself.

&lt;div [ngClass]=&quot;config.wrp_cls&quot;&gt;child component html file&lt;/div&gt;

*forked GaurangDhorda's stackblitz example to create you a demo:

Stackblitz example

This will require no additional code changing on your part.

huangapple
  • 本文由 发表于 2020年1月3日 20:43:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定