我routeProvider已經reloadOnSearch設置爲假:
$routeProvider
.when(
"/film/list",
{
templateUrl: '/film/list.html',
controller: FilmListController,
reloadOnSearch: false
}
....
)
我這樣做是因爲我不在查詢字符串更改後,不希望整個控制器重新加載,但我仍然使用它,並且url看起來像這樣:film/list?sort=isbn&order=asc&offset=0
。現在每當查詢字符串發生變化,我想從我的控制器調用一個函數。所以,我想建立一個$控制器看:
$scope.location = $location;
$scope.$watch('location.search()', function(search) {
$scope.list();
});
但是,這是行不通的。如果我設置$觀看觀察查詢字符串的單個參數的變化,那麼它的工作。但我不想爲我的查詢字符串的每個參數設置$ watch。我現在使用的解決方案是這樣的:
$scope.location = $location;
$scope.$watch('location.url()', function(url) {
if($location.path() == '/film/list')
$scope.list();
});
所以不是看的搜索,我看整個URL,然後我檢查的路徑是我在routeProvider設置爲有條件調用該函數的一個。我不喜歡這個解決方案是我必須明確鍵入$ location.path的值才能匹配。
任何人都可以提出一個更好的解決方案,或許解釋我爲什麼$看不工作的$ location.search()?
嘗試在函數中封裝location.search()。請參閱http:// stackoverflow。com/questions/13465690/changing-the-class-in-angularjs-using-url-data/13468121#13468121更多信息。 – 2013-02-26 16:46:28
做深入觀察。爲手錶添加true作爲3.參數。 – 2014-11-04 14:05:33
[AngularJS搜索更改事件]的可能重複(http://stackoverflow.com/questions/14867772/angularjs-search-change-event) – 2015-02-19 19:36:56