经常接触HTML,刚学AngularJS容易踩到的坑!
也算不上什么问题,一点注意事项吧。
ps:自学真难,尤其遇到问题还百度不到的时候,一纠结起来,一上午时间就没了!
先贴代码:
<!--part.html-->
<div>
<a href="#" ng-click="go('test')">Click me!</a>
</div>
//app.js
$urlRouterProvider.otherwise('home'); //默认路由
$stateProvider.state('home', {
url: '/home',
templateUrl: 'tpls/part.html',
controller: 'PartController'
}).state('test', {
url: '/test',
template: '<h1>Hello</h1>'
});
//controller.js
myControllers.controller('PartController', ['$scope', '$state', function ($scope, $state) {
$scope.go = function(url){
$state.go(url);
}
}]);
乍一看,没啥问题。
但结果却是:
可以看到,并没有跳转。
但是实际页面却是刷新过后的。(GIF图片帧数太低,可能看不到)
找找问题吧,
先把默认路由给注释掉看看。
真是神奇,页面跳转了两次,可以预见:
在正常$state.go()跳转之后,又进行了次跳转,这次跳转就是因为地址(index.html#)错误,而跳回了默认路由。
这个错误地址(index.html#)不用想,就知道<a href="#">就是罪魁祸首!!!
说了这么多,纠结了一上午,就是因为这个href!
ps:再重写,我就自杀!(做gif动图,老用浏览器打开,顺手就把博客窗口一起关了,我这是第五次编写了!)