Javascript 来自一个响应的多个迭代
我想显示一个详细的产品列表和一个产品型号列表,该列表在详细列表中检查,但我想只使用一个get请求。我的意思是我会得到产品的json,我会在页面中多次使用 以下是我的产品清单,详细信息如下:Javascript 来自一个响应的多个迭代,javascript,json,angularjs,single-page-application,Javascript,Json,Angularjs,Single Page Application,我想显示一个详细的产品列表和一个产品型号列表,该列表在详细列表中检查,但我想只使用一个get请求。我的意思是我会得到产品的json,我会在页面中多次使用 以下是我的产品清单,详细信息如下: <div class="box-content" ng-controller="PostsCtrl" ><div> <input type="search" ng-model="search"></div> <table class="table tabl
<div class="box-content" ng-controller="PostsCtrl" ><div>
<input type="search" ng-model="search"></div>
<table class="table table-striped table-bordered bootstrap-datatable datatable dataTable" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info">
<thead>
<tr>
<th><tags:label text="productid"/></th>
<th><tags:label text="main.category"/></th>
<th><tags:label text="sub.category"/></th>
<th><tags:label text="brand"/></th>
<th><tags:label text="model"/></th>
<th><tags:label text="sku"/></th>
<th><tags:label text="active"/></th>
<th></th>
</tr>
</thead>
<tbody>
<tr id="actionresult{{$index + 1}}" ng-repeat="post in posts | filter:search">
<td>{{post.productid}}</td>
<td>{{post.subcategory}}</td>
<td>{{post.subcategoryid}}</td>
<td>{{post.brand}}</td>
<td>{{post.model}}</td>
<td>{{post.brandid}}</td>
<td><input type="checkbox" ng-model="checked" ng-checked="post.isactive"></td>
</tr>
{{post.productid}
{{post.subcategory}
{{post.subcategory id}}
{{post.brand}}
{{post.model}}
{{post.brandid}}
型号列表:
<ul ng-controller="PostsCtrl">
<li ng-repeat="post in posts | filter:checked">{{post.model}}</li>
</ul>
- {{post.model}
这是我的控制器:
<script>
var app = angular.module("MyApp", []);
app.controller("PostsCtrl", function($scope, $http) {
$http.get('http://localhost/admin.productss/searchwithjson').
success(function(data, status, headers, config) {
$scope.posts = data;
}).
error(function(data, status, headers, config) {
});
});
</script>
<div class="box-content" ng-controller="PostsCtrl" >
...Some other html....
<table>
<tr id="actionresult{{$index + 1}}" ng-repeat="post in posts | filter:search">
<td>{{post.productid}}</td>
<td>{{post.subcategory}}</td>
<td>{{post.subcategoryid}}</td>
<td>{{post.brand}}</td>
<td>{{post.model}}</td>
<td>{{post.brandid}}</td>
<td><input type="checkbox" ng-model="post.checked"></td>
</tr>
</table>
...Some other html....
<ul>
<li ng-repeat="post in posts | filter:{checked:true}">{{post.model}}</li>
</ul>
</div>
var-app=angular.module(“MyApp”,[]);
app.controller(“PostsCtrl”,函数($scope,$http){
$http.get('http://localhost/admin.productss/searchwithjson').
成功(函数(数据、状态、标题、配置){
$scope.posts=数据;
}).
错误(函数(数据、状态、标题、配置){
});
});
我认为我在上面做的方式;我有两个要求。我不能列出检查过的型号
如何修改我的代码
谢谢。选项1: 使用控制器将两个元素放在同一个元素上:
<script>
var app = angular.module("MyApp", []);
app.controller("PostsCtrl", function($scope, $http) {
$http.get('http://localhost/admin.productss/searchwithjson').
success(function(data, status, headers, config) {
$scope.posts = data;
}).
error(function(data, status, headers, config) {
});
});
</script>
<div class="box-content" ng-controller="PostsCtrl" >
...Some other html....
<table>
<tr id="actionresult{{$index + 1}}" ng-repeat="post in posts | filter:search">
<td>{{post.productid}}</td>
<td>{{post.subcategory}}</td>
<td>{{post.subcategoryid}}</td>
<td>{{post.brand}}</td>
<td>{{post.model}}</td>
<td>{{post.brandid}}</td>
<td><input type="checkbox" ng-model="post.checked"></td>
</tr>
</table>
...Some other html....
<ul>
<li ng-repeat="post in posts | filter:{checked:true}">{{post.model}}</li>
</ul>
</div>
我不知道为什么你不能在两个地点使用相同的数据?实际上,我问如何在两个列表中使用相同的数据。我是angular的新手,所以两个HTML都在不同的控制器中。我说得对吗。如果是这样,请使用服务进行
http调用
并填充posts
数组并在控制器之间使用。如果没有,您可以在启用的控制器中的任何位置使用posts
数组。从发布的编辑中,您的HTML使用两个单独的控制器实例,因此将有两个请求。您需要将每个控件的HTML放在声明控制器的同一HTML元素中。从概念上讲,“HTTP请求可以返回‘您想要的任何内容’”。如果您想返回“关于产品的信息和型号列表”,您可以自由地这样做。只需指定“型号列表”是此请求返回的产品信息的属性之一。在javascript端,一旦获得数据,只需将其存储在方便的地方。检查模型列表如何?检查模型列表在选项1中。首先,它是否有助于解决双重请求问题?我认为您需要将ng model=“checked”更改为ng model=“post.checked”,并将filter:checked更改为filter:“checked”是的,它有助于解决双重请求问题。您可以编辑模型列表的答案吗。我试过你说的,但现在什么也看不出来。