AngularJS使用Controller實現URL跳轉
阿新 • • 發佈:2019-01-27
參考文章:https://docs.angularjs.org/guide/$location
具體寫法舉例:
1.js定義一個controller
function MyCtrl($scope, $location) {
$scope.jumpToUrl = function(path) {
//TODO:add code here
};
}
2.html裡面應用controller
<div ng-controller=‘MyCtrl’>
<button ng-click="jumpToUrl('/signin')">signin</button>
</div>
3.controller裡面TODO的位置填入
$location.path(path);
然後執行起來就可以看效果了。
假設當前頁面的url是http://127.0.0.1:8080/#/home
$location.path(path);執行後就會跳到http://127.0.0.1:8080/#/signin
如果你發現頁面不能正常跳轉,可以在$location.path(path);後面再加上一句
var curUrl = $location.absUrl(); //用來顯示url全路徑
除錯跟蹤頁面時檢視curUrl的值到底變成多少,大概就能猜出問題出在哪了。