Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular JS-仅显示JSON数据中具有最高值的字段_Angularjs_Json - Fatal编程技术网

Angularjs Angular JS-仅显示JSON数据中具有最高值的字段

Angularjs Angular JS-仅显示JSON数据中具有最高值的字段,angularjs,json,Angularjs,Json,我有一个从数据库中检索的JSON [{"id":1,"firstname":"Alan Cayetano","numbers":2},{"id":2,"firstname":"Bong Marcos","numbers":0},{"id":3,"firstname":"I Dont Care","numbers":3},{"id":4,"firstname":"toto tata","numbers":0},{"id":5,"firstname":"titi terter","numbe

我有一个从数据库中检索的JSON

[{"id":1,"firstname":"Alan Cayetano","numbers":2},{"id":2,"firstname":"Bong     Marcos","numbers":0},{"id":3,"firstname":"I Dont Care","numbers":3},{"id":4,"firstname":"toto tata","numbers":0},{"id":5,"firstname":"titi terter","numbers":0},{"id":6,"firstname":"Ian Go","numbers":0}]
这是在“结果”表中显示的结果

firstname   lastname   numbers
Alan        Cayetano    10
Bong        Marcos      4
Ian         Go          3
What        Ever        0
我只想要数值最高的数据 在这种情况下

firstname    lastname   numbers
Alan         Cayetano    10
此数据是从数据库动态获取的

[{"id":1,"firstname":"Alan Cayetano","numbers":2},{"id":2,"firstname":"Bong     Marcos","numbers":0},{"id":3,"firstname":"I Dont Care","numbers":3},{"id":4,"firstname":"toto tata","numbers":0},{"id":5,"firstname":"titi terter","numbers":0},{"id":6,"firstname":"Ian Go","numbers":0}]
我的angular.js

<script src="//code.angularjs.org/1.4.8/angular.js"></script>
<script>
    var app = angular.module('myApp', []);

    app.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('//');
    $interpolateProvider.endSymbol('//');
    });

    app.controller('customersCtrl',['$scope','$http',function($scope, $http) {
        //$http.get("http://localhost:8093/voters/voters_angular")
        $http.get("{{ path('vp_president') }}")
        .success(function (response) {
            $scope.names= JSON.parse(response);
        });
    }]);
    //console.log(names);
</script>   

var-app=angular.module('myApp',[]);
app.config(函数($interpolateProvider){
$interpolateProvider.startSymbol('/');
$interpolateProvider.endSymbol('/');
});
app.controller('customersCtrl',['$scope','$http',函数($scope,$http){
//$http.get(“http://localhost:8093/voters/voters_angular")
$http.get(“{path('vp_president')}”)
.成功(功能(响应){
$scope.names=JSON.parse(响应);
});
}]);
//console.log(名称);
桌子

<div ng-app="myApp" ng-controller="customersCtrl">
<table class="table">
    //names//

    <thead>
        <tr>
            <th>Firsname</th>
            <th>Lastname</th>
            <th>NUm</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="x in names">
            <td>//x.firstname//</td> 
            <td>//x.lastname//</td>
            <td>//x.numbers//</td>
        </tr>
    </tbody> 
</table>
</div>  

//名字//
Firsname
姓氏
号码
//x、 名字//
//x、 姓氏//
//x、 数字//
如何做到这一点?我还在学习英语
我想知道Angular的$last过滤器是否能在这方面起作用

最好在控制器中找到最大值,而不是在视图中

  function findMax(names) {
        var result = null;
        for (var i = 0; i < names.length; i++) {
            var name = names[i];
            if (result == null || name.numbers > result.numbers) {
                result = name;
            }
        }
        return result;
    }

$scope.name = findMax($scope.names);
函数findMax(名称){
var结果=null;
对于(var i=0;iresult.numbers){
结果=名称;
}
}
返回结果;
}
$scope.name=findMax($scope.name);
和html格式

<tr>
    <td>{{name.firstname}}</td> 
    <td>{{name.lastname}}</td>
    <td>{{name.numbers}}</td>
</tr>

{{name.firstname}
{{name.lastname}
{{name.numbers}}
在AngularJS中,最好在显示在视图中之前对数据进行预排序,ng repeat为它重复的每个对象创建一个观察者,在AngularJS中,观察者的数量与性能相关,如果有许多观察者,则性能最差

如果不需要显示其他值,则无需为该值创建观察程序,因此最好对数组进行预排序。

您可以执行以下操作:

<tr ng-repeat="x in names | orderBy:numbers:reverse | limitTo:1">

您可以使用:

angular.forEach($scope.names, function (value, key) { //code });
使用angular foreach,您可以逐行读取$scope.names中的数据,并使用您的逻辑获取msx值

当:

键->是位置(使用警报(键)查看信息)


value->是json行中的一个值。对于获取数据,请使用value.id、value.firstname等。

这应该按照说明进行操作


//x、 id//
//x、 名字//
//x、 数字//

为什么需要ngRepeat只显示一个值?我不熟悉Angular,我想先循环所有结果,然后在模板的其他部分中显示最高值。这将导致显示最低值。在数字后添加“:reverse”我将检查我的JSON文件。反向也不起作用。我发现JSON中的第一项无论其值如何都将始终显示。因此,它应该是“数字”。我收到此错误类型错误:无法读取Undefinedy的属性“length”。您必须在http响应中输入findMax($scope.names),这是在填充$scope.names时