在開發過程中遇到使用路由控制單頁加載頁面時,點擊菜單頁面不重新刷新的情況,angularjs認為路由沒有變化,而不會去刷新頁面,解決辦法:
1 angular.module('myApp').directive('diHref', ['$location', '$route', 2 function($location, $route) { 3 return function(scope, element, attrs) { 4 scope.$watch('diHref', function() { 5 if(attrs.diHref) { 6 element.attr('href', attrs.diHref); 7 element.bind('click', function(event) { 8 scope.$apply(function(){ 9 if($location.path() == attrs.diHref) $route.reload(); 10 }); 11 }); 12 } 13 }); 14 } 15 }]);
這樣在a標簽中使用定義的指令,即可以實現強制刷新的要求
<a di-href="/action0001" >home</a>