当我的应用程序中的状态发生变化时,我想使包含自己刷新。重新加载包含在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