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 回答
-1

你可以使用

 ng-style

https://docs.angularjs.org/api/ng/directive/ngStyle

或者

ng-class
于 2015-11-10T20:42:28.040 回答