I have two jsons: templates, and images
我有两个jsons:模板和图像
$scope.templates = [
{"id_template": 1, "name": "First Template", "group": "sport"}
{"id_template": 2, "name": "Second Template", "group": "animals"},
];
$scope.images = [
{"id_image": 1, "id_template": 1, "title":"Image Lewy"},
{"id_image": 2, "id_template": 1, "title":"Image ball"},
{"id_image": 3, "id_template": 2, "title":"Image dog"},
]
I would like to display in the view images sorted by id_template. I know that you must create a loop in the loop but do not know how ? My view.html:
我想在视图中显示按id_template排序的图像。我知道你必须在循环中创建一个循环,但不知道如何?我的view.html:
<div class="images" ng-repeat="template in templates">
{{template: name}}
Images to this template:
<ul>
<li ng-repeat="image in images | filter:template.id_template">{{image.title}}</li>
</ul>
</div>
3 个解决方案
#1
4
You can sort in angular by using orderBy filter. Please go through https://docs.angularjs.org/api/ng/filter/orderBy documentation if you want more details. From example below the result will be ascending and if you want it to be descending order add '-template_id'
您可以使用orderBy过滤器进行角度排序。如果您想了解更多详情,请浏览https://docs.angularjs.org/api/ng/filter/orderBy文档。从下面的示例中,结果将是升序,如果您希望它是降序,请添加'-template_id'
Ascending
上升
<div class="images" ng-repeat="template in templates | orderBy: 'template_id'">
{{template.name}}
Images to this template:
<ul>
<li ng-repeat="image in images | filter:{id_template:template.id_template}">{{image.title}}</li>
</ul>
</div>
Descending
降序
<div class="images" ng-repeat="template in templates | orderBy: '-template_id'">
{{template.name}}
Images to this template:
<ul>
<li ng-repeat="image in images | filter:{id_template:template.id_template}">{{image.title}}</li>
</ul>
</div>
#2
1
your almost there but have a small syntax error which is you need to print like {{template.name}}
instead of {{template: name}}
你几乎在那里,但有一个小的语法错误,你需要打印像{{template.name}}而不是{{template:name}}
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
<script src="script.js"></script>
</head>
<body ng-controller="MainCtrl">
<div class="images" ng-repeat="template in templates">
{{template.name}}
Images to this template:
<ul>
<li ng-repeat="image in images | filter:{id_template:template.id_template}">{{image.title}}</li>
</ul>
</div
</body>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.templates = [
{"id_template": 1, "name": "First Template", "group": "sport"},
{"id_template": 2, "name": "Second Template", "group": "animals"}
];
$scope.images = [
{"id_image": 1, "id_template": 1, "title":"Image Lewy"},
{"id_image": 2, "id_template": 1, "title":"Image ball"},
{"id_image": 3, "id_template": 2, "title":"Image dog"},
]
});
</script>
</html>
#3
0
you must use OrderBy for sorting items in angularjs
您必须使用OrderBy对angularjs中的项目进行排序
<li ng-repeat="image in images | orderBy:template.id_template">{{image.title}}</li>
#1
4
You can sort in angular by using orderBy filter. Please go through https://docs.angularjs.org/api/ng/filter/orderBy documentation if you want more details. From example below the result will be ascending and if you want it to be descending order add '-template_id'
您可以使用orderBy过滤器进行角度排序。如果您想了解更多详情,请浏览https://docs.angularjs.org/api/ng/filter/orderBy文档。从下面的示例中,结果将是升序,如果您希望它是降序,请添加'-template_id'
Ascending
上升
<div class="images" ng-repeat="template in templates | orderBy: 'template_id'">
{{template.name}}
Images to this template:
<ul>
<li ng-repeat="image in images | filter:{id_template:template.id_template}">{{image.title}}</li>
</ul>
</div>
Descending
降序
<div class="images" ng-repeat="template in templates | orderBy: '-template_id'">
{{template.name}}
Images to this template:
<ul>
<li ng-repeat="image in images | filter:{id_template:template.id_template}">{{image.title}}</li>
</ul>
</div>
#2
1
your almost there but have a small syntax error which is you need to print like {{template.name}}
instead of {{template: name}}
你几乎在那里,但有一个小的语法错误,你需要打印像{{template.name}}而不是{{template:name}}
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
<script src="script.js"></script>
</head>
<body ng-controller="MainCtrl">
<div class="images" ng-repeat="template in templates">
{{template.name}}
Images to this template:
<ul>
<li ng-repeat="image in images | filter:{id_template:template.id_template}">{{image.title}}</li>
</ul>
</div
</body>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.templates = [
{"id_template": 1, "name": "First Template", "group": "sport"},
{"id_template": 2, "name": "Second Template", "group": "animals"}
];
$scope.images = [
{"id_image": 1, "id_template": 1, "title":"Image Lewy"},
{"id_image": 2, "id_template": 1, "title":"Image ball"},
{"id_image": 3, "id_template": 2, "title":"Image dog"},
]
});
</script>
</html>
#3
0
you must use OrderBy for sorting items in angularjs
您必须使用OrderBy对angularjs中的项目进行排序
<li ng-repeat="image in images | orderBy:template.id_template">{{image.title}}</li>