當我的應用程序中的狀態發生變化時,我想使包含自己刷新。重新加載包含在AngularJS中的狀態變化
例子:
<div ng-include="'partials/search-form.html'"></div>
全球聽衆:
phonecatApp.run(function ($state,$rootScope, $log) {
$rootScope.$state = $state;
$rootScope.$on('$stateChangeSuccess', function(){
if(!$state.includes('search')) {
// refresh the search-form include in the header
}
});
});
我怎樣才能做到這一點?因爲表單不在ui視圖之外,當我更改頁面時,它仍然具有上一個搜索查詢的值,因爲它仍處於之前的狀態......我如何刷新它以便更新?
注:創建另一個UI視圖是不溶液作爲這種形式僅僅是一個簡單的包括在所有網頁上的報頭所示,並且不涉及任何狀態等
更新:所以你可以看到我的形式和控制器做:
phonecatControllers.controller('SearchCtrl', function($rootScope, $scope, $state, $location) {
$scope.query = ($state.includes('search') ? $location.search()['q'] : '');
$scope.filterQuery = ($state.includes('search') ? $location.search()['q'] : '');
if(!$scope.query){
$location.search('q', null);
}
$scope.queryChanged = function() {
if($scope.query){
$state.go('search', {'q': $scope.query});
} else {
$location.search('q', null);
}
$scope.filterQuery = $scope.query;
}
});
<form class="form-search" ng-controller="SearchCtrl" ng-submit="queryChanged()">
<input name="q" ng-model="query" id="filter" type="text">
<button type="submit" class="btn">Search</button>
</form>
更新:我能想出最好的辦法,將清空輸入字段像這樣:
var q = document.getElementsByName('q');
q.setAttribute('value', '');
當狀態改變時不是搜索狀態。這會自動觸發輸入上的變化事件,然後使其有它的NG-髒類中刪除等
你不能重置模型嗎? – Th0rndike 2014-09-04 10:12:07
@ Th0rndike你能舉個例子嗎?記住這需要是全球性的。 – Cameron 2014-09-04 10:13:40
我的意思是:如果您將此表單包含在頁面中,那麼該頁面可能具有表單中顯示的數據的模型(即使它包含在內)。在更改頁面重置模型之前,可能使用statechange偵聽器不是最佳解決方案。考慮到我做了很多假設,因爲你的問題中只有很少的代碼。 – Th0rndike 2014-09-04 10:19:02