質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

Q&A

解決済

2回答

1960閲覧

動的にクラスを指定する方法

kcysne

総合スコア6

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

0グッド

0クリップ

投稿2017/12/04 07:15

編集2017/12/04 07:28

###前提・実現したいこと
*ngFor="let sensor of sensors"で取得したデータの中の値に応じてクラスを指定する方法がわかりません。
isErrorがtrueの時にクラスをerrorに指定したいです。

###該当のソースコード

html

1<table class="sensor-table"> 2 <thead> 3 <tr> 4 <td class="header" colspan="10">一覧 5 <button class="menu_btn" type="button" (click)="onClickMenu()"></button> 6 </td> 7 </tr> 8 <tr> 9 <th></th> 10 <th>番号</th> 11 <th>エラー</th> 12 <th>警報</th> 13 <th>場所</th> 14 <th></th> 15 <th>温度(℃)</th> 16 <th>湿度(%)</th> 17 <th>日時</th> 18 <th></th> 19 </tr> 20 </thead> 21 <tbody> 22 <tr *ngFor="let sensor of sensors"> 23 <td> 24 <button class="color_btn" type="button" (click)="onClickColor(sensor)"></button> 25 </td> 26 <td class="station">{{sensor.station}}</td> 27 <td>{{sensor.error}}</td> 28 <td>{{sensor.alarm}}</td> 29 <td class="location">{{sensor.location}}</td> 30 <td> 31 <button class="history_btn" type="button" (click)="onClickHistory(sensor)">履歴</button> 32 </td> 33 <td class="temperature">{{sensor.temperature}}</td> 34 <td class="humidity">{{sensor.humidity}}</td> 35 <td>{{sensor.date}}</td> 36 <td class="extra"></td> 37 </tr> 38 </tbody> 39</table> 40

###表示用

typescript

1export const SENSORS: Sensor[] = [ 2 { 3 color: null, 4 station: 1, 5 error: null, 6 isError: false, 7 alarm: 0, 8 isAlarm: true, 9 location: '第1', 10 temperature: 92, 11 humidity: 65.2, 12 date: '11/11 11:11' 13 }, 14 { 15 color: null, 16 station: 2, 17 error: null, 18 isError: false, 19 alarm: null, 20 isAlarm: false, 21 location: '第2', 22 temperature: 45, 23 humidity: 65.2, 24 date: '11/11 11:11' 25 }, 26 { 27 color: null, 28 station: 3, 29 error: null, 30 isError: false, 31 alarm: null, 32 isAlarm: false, 33 location: '第3', 34 temperature: 45, 35 humidity: 65.2, 36 date: '11/11 11:11' 37 }, 38 { 39 color: null, 40 station: 4, 41 error: 0, 42 isError: true, 43 alarm: null, 44 isAlarm: false, 45 location: '第4', 46 temperature: 24, 47 humidity: 65.2, 48 date: '11/11 11:11' 49 }, 50 { 51 color: null, 52 station: 5, 53 error: 0, 54 isError: true, 55 alarm: 0, 56 isAlarm: true, 57 location: '第5', 58 temperature: 51, 59 humidity: 65.2, 60 date: '11/11 11:11' 61 }, 62 { 63 color: null, 64 station: 6, 65 error: null, 66 isError: false, 67 alarm: null, 68 isAlarm: false, 69 location: '第6', 70 temperature: 49, 71 humidity: 65.2, 72 date: '11/11 11:11' 73 }, 74 { 75 color: null, 76 station: null, 77 error: null, 78 isError: false, 79 alarm: null, 80 isAlarm: false, 81 location: null, 82 temperature: null, 83 humidity: null, 84 date: null 85 } 86]; 87

###試したこと
[ngClass]="{'error': sensor.isError}"

###補足情報(言語/FW/ツール等のバージョンなど)
言っている意味が分からなかったらすみません。
指摘していただければ補足いたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

補足:この場合こういう指定もできます

html

1<tr *ngFor="let sensor of sensors" [ngClass.error]="sensor.isError">

追加補足:文字列の代入だと他のclassも当てたいなどの場合、拡張性にかけるので、複数指定の場合はこの記述がオススメです。

html

1<tr *ngFor="let sensor of sensors" [ngClass]="{'error': sensor.isError, 'empty': sensor.isEmpty}">

投稿2017/12/06 08:21

編集2017/12/06 08:26
Everatch

総合スコア241

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kcysne

2017/12/06 08:24

おおお、スッキリしていて分かりやすいですね!! これからはこちらを使っていこうと思います!! ありがとうございました。
kcysne

2017/12/06 08:26

重ねての補足ありがとうございます。 とても勉強になります!!
guest

0

自己解決

html

1<tr *ngFor="let sensor of sensors;" [ngClass]="(sensor.isError)?'error':''">

自己解決しました。
これで動きました。

投稿2017/12/04 08:28

kcysne

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問