2012-10-04 19 views
1

好的,所以我有一個Angular模塊按照頁面加載的意圖工作 - 像site.com#/ catalog/1234這樣的網址。 $資源獲取重新加載視圖的數據,但不是在hashchange上。

我需要添加什麼來獲得hashchange來觸發新的獲取?

angular.module('Catalog', ['ngResource']) 
.config(function($routeProvider, $locationProvider) { 
    $routeProvider 
     .when('/catalog/:categoryid', { 
      controller: 'CatalogListCtrl', 
      templateUrl:'/layout/responsive/html/catalogListView.html' 
     }) 
     .otherwise({ redirectTo:'/' }); 
}) 
.factory('CatalogList', function($resource, $routeParams) { 
    var list = $resource('/test/products.json?query=:categoryid', { categoryid : $routeParams.categoryid }, { 
      query: { method: 'GET', isArray : true } 
    }); 
    return list; 
}) 
.controller('CatalogListCtrl', function ($scope, CatalogList) { 
    $scope.products = CatalogList.query(); 
}); 
+0

你是什麼意思hashchange? – Narretz

+0

你不能真的注入$ routeParams到服務聲明中,我不這麼認爲。只有控制器。 –

+1

@blesh是正確的。但是,您可以注入$ route並通過$ route.current訪問當前參數。 – qualidafial

回答

2

難道是您爲服務設置的標準綁定混淆了角?

我會改變你的代碼的方式,也許你可以嘗試它。

angular.module('Catalog', ['ngResource']) 
.config(function($routeProvider, $locationProvider) { 
    $routeProvider 
     .when('/catalog/:categoryid', { 
      controller: 'CatalogListCtrl', 
      templateUrl:'/layout/responsive/html/catalogListView.html' 
     }) 
     .otherwise({ redirectTo:'/' }); 
}) 
.factory('CatalogList', function($resource) { 
    return list = $resource('/test/products.json?query=:categoryid', {}, {}); 
}) 
.controller('CatalogListCtrl', function ($scope, $routeParams, CatalogList) { 
    $scope.products = CatalogList.query({categoryid: $routeParams.categoryid}); 
}); 

我還刪除了一些只重複默認值的代碼。讓我知道它是否有效。

+0

我認爲這可能是正確的... $ routeParams真的是要被控制器使用,而不是服務。但是,您在可以消失的服務聲明中留下了$ routeParams。 –

+0

感謝,作品像魅力。 $ routeParams適用於控制器 - 必須記住這一點。 再次感謝! –

+0

@blesh哦是的你是對的:) $ routeParams是不需要的!謝謝。 –