AngularJS利用Controller完成URL跳转

 更新时间:2016年08月09日 11:40:52   投稿:daisy  
本文的主要内容是介绍在AngularJS中怎样利用Controller实现URL跳转,本文给出了实例代码,简单明了,有需要的可以参考学习。

GPT4.0+Midjourney绘画+国内大模型 会员永久免费使用!
如果你想靠AI翻身,你先需要一个靠谱的工具!

具体写法举例:

1.js定义一个controller

1
2
3
4
5
6
7
8
9
function MyCtrl($scope, $location) {
 
  $scope.jumpToUrl = function(path) {
 
    //TODO:add code here
 
  };
 
}

2.html里面应用controller

1
2
3
4
5
<div ng-controller=‘MyCtrl'>
 
  <button ng-click="jumpToUrl('/signin')">signin</button>
 
</div>

3.controller里面TODO的位置填入

1
$location.path(path);

然后运行起来就可以看效果了。

假设当前页面的url是:http://127.0.0.1:8080/#/home

$location.path(path);执行后就会跳到http://127.0.0.1:8080/#/signin

如果你发现页面不能正常跳转,可以在$location.path(path);后面再加上一句

1
var curUrl = $location.absUrl(); //用来显示url全路径

调试跟踪页面时查看curUrl的值到底变成多少,大概就能猜出问题出在哪了。

好了,以上就是在AngularJS利用Controller完成URL跳转的全部内容,希望本文对大家学习AngularJS有所帮助。

蓄力AI

微信公众号搜索 “ 脚本之家 ” ,选择关注

程序猿的那些事、送书等活动等着你

相关文章

最新评论