【发布时间】:2015-06-28 18:37:43
【问题描述】:
我是 AngularJs 的新手,我正在获取格式为的 json 数据:
[
{
'StudentName':'abc',
'maths':'0',
'english':'0',
'economics':'0',
}
]
我想计算每个学生的分数,如果分数低于 40%,那么表格行应该是红色的,否则应该是绿色的。 我努力了。 HTML
<div ng-app="MyApp" ng-controller="con1">
<table id="table1">
<tr>
<th>student Name</th>
<th>History Marks</th>
<th>Maths Marks</th>
<th>Economics Marks</th>
<th>Percentage</th>
</tr>
<tr ng-repeat="x in data" ng-if="{{co(per(x))}}" ng-class="pass">
<td>{{x.StudentName}}</td>
<td>{{x.maths}}</td>
<td>{{x.economics}}</td>
<td>{{x.english}}</td>
<td>{{per(x)}}%</td>
</tr>
</table>
脚本
var app = angular.module('MyApp', []);
app.controller('con1', function ($scope, $http) {
$http.get('/ajax/data').success(function (res) { $scope.data = res; });
$scope.per = function (x) {
avg = ((parseInt(x.maths) + parseInt(x.economics) + parseInt(x.english)) * 100) / 300;
return avg;
};
$scope.co = function (x) { (x > 40) ? k = 1 : k = 0; return (k); }
});
css
.pass{background:green;}
.fail{background:red;}
我得到百分比,但根据百分比我没有得到行颜色。
【问题讨论】:
-
刚接触 angularjs 时最好不要使用任何 jQuery
-
我实际上没有看到 jQuery 被用在了哪里。
-
我认为很多人将 jQuery 与 Javascript 混淆了。
-
Jquery 和 AngularJS 不能共存吗? :) 还没有开始使用 Angular,但我正在使用 Jquery
-
@JaredT,是的,但是,你会遇到很多 Angular 纯粹主义者说你永远不应该同时使用 jQuery 和 Angular。我不同意。从理论上讲,这是一个很棒的概念。实际上,它只是让一些事情变得更容易使用 jQuery。
标签: javascript jquery angularjs