我有一个输入字段
<md-input-container ng-if="gpa.grade_type==2">
<input type="text" ng-model="gpa.grade_percent" aria-label="Grade">
</md-input-container>
我想查看gpa.grade_percent&#34; percent_grade_from&#34;:93 - &#34; percent_grade_to&#34;:96,JSON中的值,其中适合的间隔 所以我必须在下一个字段中显示间隔相关的比例。
<td>
<span ng-bind="gpa.grade_scale"></span>
</td>
JSON:
[
{
"id": 2,
"letter_grade": "A",
"percent_grade_from": 93,
"percent_grade_to": 96,
"scale": "4.0",
"honors_gpa_from": "4.3",
"honors_gpa_to": "4.4",
"ap_ib_gpa_from": "4.8",
"ap_ib_gpa_to": "4.9"
},
{
"id": 3,
"letter_grade": "A-",
"percent_grade_from": 90,
"percent_grade_to": 92,
"scale": "3.7",
"honors_gpa_from": "3.9",
"honors_gpa_to": "4.2",
"ap_ib_gpa_from": "4.4",
"ap_ib_gpa_to": "4.7"
},
{
"id": 4,
"letter_grade": "B+",
"percent_grade_from": 87,
"percent_grade_to": 89,
"scale": "3.3",
"honors_gpa_from": "3.6",
"honors_gpa_to": "3.8",
"ap_ib_gpa_from": "4.1",
"ap_ib_gpa_to": "4.3"
},
{
"id": 5,
"letter_grade": "B",
"percent_grade_from": 83,
"percent_grade_to": 86,
"scale": "3.0",
"honors_gpa_from": "3.3",
"honors_gpa_to": "3.5",
"ap_ib_gpa_from": "3.8",
"ap_ib_gpa_to": "4.0"
},
{
"id": 6,
"letter_grade": "B-",
"percent_grade_from": 80,
"percent_grade_to": 82,
"scale": "2.7",
"honors_gpa_from": "2.0",
"honors_gpa_to": "3.2",
"ap_ib_gpa_from": "3.4",
"ap_ib_gpa_to": "3.7"
},
{
"id": 7,
"letter_grade": "C+",
"percent_grade_from": 77,
"percent_grade_to": 79,
"scale": "2.3",
"honors_gpa_from": "2.6",
"honors_gpa_to": "2.8",
"ap_ib_gpa_from": "3.1",
"ap_ib_gpa_to": "3.3"
},
{
"id": 8,
"letter_grade": "C",
"percent_grade_from": 73,
"percent_grade_to": 76,
"scale": "2.0",
"honors_gpa_from": "2.3",
"honors_gpa_to": "2.5",
"ap_ib_gpa_from": "2.8",
"ap_ib_gpa_to": "3.0"
},
{
"id": 9,
"letter_grade": "C-",
"percent_grade_from": 70,
"percent_grade_to": 72,
"scale": "1.7",
"honors_gpa_from": "1.9",
"honors_gpa_to": "2.2",
"ap_ib_gpa_from": "2.4",
"ap_ib_gpa_to": "2.7"
},
{
"id": 10,
"letter_grade": "D+",
"percent_grade_from": 67,
"percent_grade_to": 69,
"scale": "1.3",
"honors_gpa_from": "1.6",
"honors_gpa_to": "1.8",
"ap_ib_gpa_from": "2.1",
"ap_ib_gpa_to": "2.3"
},
{
"id": 11,
"letter_grade": "D",
"percent_grade_from": 65,
"percent_grade_to": 66,
"scale": "1.0",
"honors_gpa_from": "0.1",
"honors_gpa_to": "1.5",
"ap_ib_gpa_from": "0.1",
"ap_ib_gpa_to": "2.0"
},
{
"id": 12,
"letter_grade": "E/F",
"percent_grade_from": 0,
"percent_grade_to": 65,
"scale": "0.0",
"honors_gpa_from": "0.0",
"honors_gpa_to": null,
"ap_ib_gpa_from": "0.0",
"ap_ib_gpa_to": null
},
{
"id": 1,
"letter_grade": "A+",
"percent_grade_from": 97,
"percent_grade_to": 100,
"scale": "4.0",
"honors_gpa_from": "4.5",
"honors_gpa_to": "4.5",
"ap_ib_gpa_from": "5.0",
"ap_ib_gpa_to": "5.0"
}
]
答案 0 :(得分:1)
angular.module('app', []).controller('MyController', ['$scope', function($scope) {
$scope.gpa = {grade_percent:68};
var data = [
{
"id": 2,
"letter_grade": "A",
"percent_grade_from": 93,
"percent_grade_to": 96,
"scale": "4.0",
"honors_gpa_from": "4.3",
"honors_gpa_to": "4.4",
"ap_ib_gpa_from": "4.8",
"ap_ib_gpa_to": "4.9"
},
{
"id": 3,
"letter_grade": "A-",
"percent_grade_from": 90,
"percent_grade_to": 92,
"scale": "3.7",
"honors_gpa_from": "3.9",
"honors_gpa_to": "4.2",
"ap_ib_gpa_from": "4.4",
"ap_ib_gpa_to": "4.7"
},
{
"id": 4,
"letter_grade": "B+",
"percent_grade_from": 87,
"percent_grade_to": 89,
"scale": "3.3",
"honors_gpa_from": "3.6",
"honors_gpa_to": "3.8",
"ap_ib_gpa_from": "4.1",
"ap_ib_gpa_to": "4.3"
},
{
"id": 5,
"letter_grade": "B",
"percent_grade_from": 83,
"percent_grade_to": 86,
"scale": "3.0",
"honors_gpa_from": "3.3",
"honors_gpa_to": "3.5",
"ap_ib_gpa_from": "3.8",
"ap_ib_gpa_to": "4.0"
},
{
"id": 6,
"letter_grade": "B-",
"percent_grade_from": 80,
"percent_grade_to": 82,
"scale": "2.7",
"honors_gpa_from": "2.0",
"honors_gpa_to": "3.2",
"ap_ib_gpa_from": "3.4",
"ap_ib_gpa_to": "3.7"
},
{
"id": 7,
"letter_grade": "C+",
"percent_grade_from": 77,
"percent_grade_to": 79,
"scale": "2.3",
"honors_gpa_from": "2.6",
"honors_gpa_to": "2.8",
"ap_ib_gpa_from": "3.1",
"ap_ib_gpa_to": "3.3"
},
{
"id": 8,
"letter_grade": "C",
"percent_grade_from": 73,
"percent_grade_to": 76,
"scale": "2.0",
"honors_gpa_from": "2.3",
"honors_gpa_to": "2.5",
"ap_ib_gpa_from": "2.8",
"ap_ib_gpa_to": "3.0"
},
{
"id": 9,
"letter_grade": "C-",
"percent_grade_from": 70,
"percent_grade_to": 72,
"scale": "1.7",
"honors_gpa_from": "1.9",
"honors_gpa_to": "2.2",
"ap_ib_gpa_from": "2.4",
"ap_ib_gpa_to": "2.7"
},
{
"id": 10,
"letter_grade": "D+",
"percent_grade_from": 67,
"percent_grade_to": 69,
"scale": "1.3",
"honors_gpa_from": "1.6",
"honors_gpa_to": "1.8",
"ap_ib_gpa_from": "2.1",
"ap_ib_gpa_to": "2.3"
},
{
"id": 11,
"letter_grade": "D",
"percent_grade_from": 65,
"percent_grade_to": 66,
"scale": "1.0",
"honors_gpa_from": "0.1",
"honors_gpa_to": "1.5",
"ap_ib_gpa_from": "0.1",
"ap_ib_gpa_to": "2.0"
},
{
"id": 12,
"letter_grade": "E/F",
"percent_grade_from": 0,
"percent_grade_to": 65,
"scale": "0.0",
"honors_gpa_from": "0.0",
"honors_gpa_to": null,
"ap_ib_gpa_from": "0.0",
"ap_ib_gpa_to": null
},
{
"id": 1,
"letter_grade": "A+",
"percent_grade_from": 97,
"percent_grade_to": 100,
"scale": "4.0",
"honors_gpa_from": "4.5",
"honors_gpa_to": "4.5",
"ap_ib_gpa_from": "5.0",
"ap_ib_gpa_to": "5.0"
}
];
$scope.find = function(){
$scope.gpa.grade_scale = (data.filter(function(x){
return x.percent_grade_from <= $scope.gpa.grade_percent && x.percent_grade_to >= $scope.gpa.grade_percent;
})[0] || {}).scale;
}
$scope.find();
}])
&#13;
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<div ng-app='app' ng-controller="MyController">
<input type="text" ng-model="gpa.grade_percent" aria-label="Grade" ng-change='find()'>
<br/>
<span ng-bind='gpa.grade_scale'></span>
</div>
&#13;