0

私は AngularJS Ionic Meteor アプリを開発しています。その内容 (フロート) に応じて、イオン カードのボタン ボックスの背景色を変更する必要があります。範囲は次のとおりです。

data<=80
81 < data <= 160
161 < data <= 233
234 < data<= 317
318 < data <= 400.

それを行う CSS の方法、または代わりに AngularJS の方法はありますか?

4

2 に答える 2

2

ngClassを使用できます。CSS の背景色のプロパティを設定し、コントローラーで適切なクラスを設定するだけです。次に例を示します。

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
  $scope.submit = function() {
    if ($scope.data <= 80) $scope.rangeColor = "red";
    // Add more conditional statements
    else $scope.rangeColor = "blue";
  }
}
.card {
  border-style: solid;
}
.red {
  background-color: red;
}
.blue {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <h4>Data:</h4>
    <form ng-submit="submit()">
      <input type="text" name="data" ng-model="data" required>
      <input type="submit" id="submit" value="Submit" />
    </form>
    <br />
    <div ng-class="rangeColor" class="card">
      <div class="item item-text-wrap">
        This is a basic Card which contains an item that has wrapping text.
      </div>
    </div>
  </div>
</body>

HTML 要素に条件ステートメントを実装することもできます。

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {

}
.card {
  border-style: solid;
}
.red {
  background-color: red;
}
.blue {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <h4>Data:</h4>
    <input type="text" name="data" ng-model="data" required>
    <br />
    <br />
    <div ng-class="{'red': data <= 80, 'blue': data > 80}" class="card">
      <div class="item item-text-wrap">
        This is a basic Card which contains an item that has wrapping text.
      </div>
    </div>
  </div>
</body>

于 2015-11-10T20:43:29.547 に答える