2017-04-12 1 views
0

式内の指定された値をチェックする式を使用しようとしています。私がこれを行うために管理している:クリーナーアプローチこのNgClassで使用するOR式として複数の文字列値を確認

+0

論理をプロパティまたはメソッドに配置し、一致するターゲット文字列があれば真を返すことができます。もう一つのアプローチは、すべての文字列を配列リテラルに入れ、 '[" Relationships "、etc] .indexOf(title)!== -1'を使うことですが、構文がその位置でサポートされているかどうかは思い出せません。 –

答えて

0

探し

<h2 class="heading" [ngClass]='{"redBackground" : info?.title == "Relationships" || info?.title == "In The Anime" || info?.title == "Anime" || info?.title == "Appearance" || info?.title == "Relationships" || info?.title == "In The Anime"}'>{{ info.title }}</h2> 

特に短いです:(IEではサポートされていません)

<h2 class="heading" [ngClass]='{"redBackground" : ["Relationships", "In The Anime", "Anime", "Appearance", "Relationships", "In The Anime"].indexOf(info?.title) >= 0}'>{{ info.title }}</h2> 

またはhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

<h2 class="heading" [ngClass]='{"redBackground" : ["Relationships", "In The Anime", "Anime", "Appearance", "Relationships", "In The Anime"].includes(info?.title)}'>{{ info.title }}</h2> 

私はそれをテンプレートにあまりにも多くのコードを持つ代わりにTSコードに移動することを提案します。

関連する問題