我需要一些帮助来显示来自 $watch 的 ng-repeat 指令中的更新结果
- ProductsCtrl 正在监视产品类型的变化,当它检测到一个变化时,它会搜索该类型的产品。
- 这肯定会检测到产品类型的变化,并且我从 updateProducts 函数中得到了结果,但我得到的产品没有显示出来。
- 我认为将 $scope.productService.products 映射到 ProductService.products 意味着它将自动更新。
我错过了什么吗?
谢谢你。
控制器.js
.controller('ProductsCtrl',['$scope','ProductService', '$location', 'SessionDataService', function($scope, ProductService, $location, SessionDataService){
$scope.productService = ProductService;
$scope.productService.products = ProductService.products;
$scope.sessionDataService = SessionDataService;
$scope.productType = SessionDataService.productType;
$scope.$watch('productType', function(productTypeNew, productTypeOld){
//update products
console.log('ProductsCtrl: watch : New Val - '+ productTypeNew + ', Old Val - ' + productTypeOld);
$scope.productService.updateProducts(productTypeNew);
console.log("ProductsCtrl: watch : Products retrieved - " + $scope.productService.products);
}, true);
}]);
服务.js
.factory('Product',['$resource', function($resource){
return $resource('/products/:id', {
id: '@id'
});
}])
.factory('ProductService', ['Product', function(Product){
var products = [];
function updateProducts(productType){
console.log("ProductService updateProducts with product type " + productType);
Product.query({product_type: productType},
function (data) {
console.log("ProductService updateProducts returned " + data);
var retProducts = data;
angular.copy(retProducts, products);
//console.log(retProducts);
});
}
return {
products: products,
updateProducts: updateProducts
}}])
html
<div ng-controller="ProductsCtrl">
<tr ng-repeat="product in productService.products">
<td>{{product.title}}</td>
<td>{{product.retailler}}</td>
</tr>
</div>