想象这样一个场景,咱们经过菜单导航进入一个操做页面,当在页面中已经录入或选择了一些信息,发现根本不是想要的,但愿可以使页面快速恢复的初始状态,这时候一个比较理想的选择是再次点击下菜单栏里的对应项。javascript
若是你的菜单是使用这种方式,那么点击当前的菜单,页面将不会有任何反应java
<li ng-repeat="m in menus" ui-sref-active="li_active"> <a href="#" ui-sref="{{m.state}}"> <span ng-bind="m.name"></span> </a> </li>
去查找资料,有不少信息提示咱们使用ui-sref-opts指令来设置reload参数,将代码改为这样,测试下会发现当前页面确实是刷新了,可是整个页面也同时都刷新了git
<li ng-repeat="m in menus" ui-sref-active="li_active"> <a href="#" ui-sref="{{m.state}}" ui-sref-opts="{reload:true}"> <i ng-class="message.image"></i> <span ng-bind="message.menuCName"></span> </a> </li>
其实,reload参数不光能够传递一个布尔型参数,还能够接受string和object型参数,若是只是想刷新当前的路由页面,而不去连带刷新父路由,咱们能够把reload的参数值设置为当前路由对应的字符串,代码是这样:github
<li ng-repeat="m in menus" ui-sref-active="li_active"> <a href="#" ui-sref="{{m.state}}" ui-sref-opts="{reload:'{{m.state}}'}"> <i ng-class="message.image"></i> <span ng-bind="message.menuCName"></span> </a> </li>
固然, 上面说的场景只是一种状况,咱们在程序的控制逻辑中也会用到刷新页面的状况,方法相似,一种能够经过$state.go的方式跳转路由,一样可使用这个参数来处理;另一种能够直接使用$state.reload,直接调用$state.reload()是加载整个页面,$state.reload('currentState')则是加载当前路由,这些在源码的注释中都有清晰的说明promise
/** * @ngdoc function * @name ui.router.state.$state#reload * @methodOf ui.router.state.$state * * @description * A method that force reloads the current state. All resolves are re-resolved, * controllers reinstantiated, and events re-fired. * * @example * <pre> * var app angular.module('app', ['ui.router']); * * app.controller('ctrl', function ($scope, $state) { * $scope.reload = function(){ * $state.reload(); * } * }); * </pre> * * `reload()` is just an alias for: * <pre> * $state.transitionTo($state.current, $stateParams, { * reload: true, inherit: false, notify: true * }); * </pre> * * @param {string=|object=} state - A state name or a state object, which is the root of the resolves to be re-resolved. * @example * <pre> * //assuming app application consists of 3 states: 'contacts', 'contacts.detail', 'contacts.detail.item' * //and current state is 'contacts.detail.item' * var app angular.module('app', ['ui.router']); * * app.controller('ctrl', function ($scope, $state) { * $scope.reload = function(){ * //will reload 'contact.detail' and 'contact.detail.item' states * $state.reload('contact.detail'); * } * }); * </pre> * * `reload()` is just an alias for: * <pre> * $state.transitionTo($state.current, $stateParams, { * reload: true, inherit: false, notify: true * }); * </pre> * @returns {promise} A promise representing the state of the new transition. See * {@link ui.router.state.$state#methods_go $state.go}. */
另外,须要重点说明下,你使用的ui-router版本须要是0.2.14以上的,不然这样写仍然会刷新整个页面,貌似是以前版本的bug。app
使用0.2.x版本的建议能够直接升级到0.2.18。测试