问题
我正在尝试使用(最初)以下功能构建产品列表:
- 服务端分页
- 服务器端过滤
我目前有很多问题,但主要是我似乎无法以最好的方式分离视图。每当页面更改时,类别列表就会更新(如果选中任何项目,则它们未选中)。
如何仅在页面加载时加载类别列表?
谢谢
代码
索引.html:
<div ng-app="relv">
<div ng-view></div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script src="//cdn.jsdelivr.net/angularjs/1.0.2/angular-resource.min.js"></script>
<script src="/angular/app.js"></script>
<script src="/angular/services.js"></script>
<script src="/angular/controllers.js"></script>
<script src="/angular/filters.js"></script>
<script src="/angular/directives.js"></script>
应用程序.js:
'use strict';
angular.module('relv', ['relv.filters', 'relv.services', 'relv.directives']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/products', {templateUrl: '/angular/partials/product_list.html', controller: ProductListCtrl});
$routeProvider.when('/products/:page', {templateUrl: '/angular/partials/product_list.html', controller: ProductListCtrl});
$routeProvider.otherwise({redirectTo: '/products'});
}]);
产品列表.html:
<div id="category_list">
<label ng-repeat="category in categories">
<input type="checkbox" name="category" ng-model="filterCategories[category.id]"> {{ category.name }}
</label>
</div>
{{ filterCategories }}
Product list, page {{ page }}.
<br><br>
<ul>
<li ng-repeat="product in products">
<a href="#/product/{{ product.id }}">{{ product.name }}</a><br>
<span ng-repeat="category in product.categories">{{ category.name }}</span>
</li>
</ul>
<a href="#/products/{{ page-1 }}">Previous page</a>
<a href="#/products/{{ page+1 }}">Next page</a>
控制器.js:
'use strict';
function ProductListCtrl($routeParams, $scope, ProductList, CategoryList) {
$scope.page = $routeParams.page ? parseInt($routeParams.page) : 1;
$scope.products = ProductList.query({'page': $scope.page});
$scope.categories = CategoryList.query();
$scope.filterCategories = {};
}
ProductListCtrl.$inject = ['$routeParams', '$scope', 'ProductList', 'CategoryList'];
服务:js:
'use strict';
angular.module('relv.services', ['ngResource']).
factory('Product', function($resource){
return $resource('http://endpoint/api_dev.php/products/:productId.json', {}, {
query: {method:'GET', params:{lang:'en'}, isArray:false}
});
}).
factory('ProductList', function($resource){
return $resource('http://endpoint/api_dev.php/products.json', {}, {
query: {method:'GET', params:{lang:'en', page: ':page'}, isArray:true}
});
}).
factory('CategoryList', function($resource){
return $resource('http://endpoint/api_dev.php/categories.json', {}, {
query: {method:'GET', params:{lang:'en'}, isArray:true}
});
})
;