2017-04-25 6 views
0

ずにhtml要素を削除し、私は、あなたがアンギュラ - クラス

<my-component myClass="hello-class"></my-component> 

とコンポーネントのHTMLで、HTMLにクラスを渡すことができコンポーネントを持って

<div class="{{myClass}}"> Div with the class</div> 
<div>A div without the class</div> 

しかし、場合によってはどこがありますクラスをmyClassに渡すことはできません。そのようなシナリオでは

、私は、ユーザーからのクラスの入力を必要とdiv要素を削除したい、

<my-component></my-component>(何のクラスは渡されません)

だけではありませんdiv要素は必要ありません示していクラス。

<div>A div without the class</div> 

私はこれを行うには、HTMLに渡すことができる条件があり、

例:

<div *ngIf="myClass-is-not-empty" class="{{myClass}}"> Div with the class</div> 
<div>A div without the class</div> 

答えて

1
<div *ngIf="myClass" class="{{myClass}}"> Div with the class</div> 
<div>A div without the class</div> 
0
<div ng-if="!!myClass" class={{myClass}}">