【发布时间】:2015-02-10 00:18:37
【问题描述】:
我有以下angularjs 路由设置。
app.js
.config(function($stateProvider, $urlRouterProvider){
//some code
.state('app.edit', {
url: '/edit/:recipeId',
views: {
'menuContent': {
templateUrl: 'templates/recipes/edit.html',
controller: 'editRecipeCtrl'
}
}
//some code
});
$urlRouterProvider.otherwise('/app/home');
查看
<a ng-click="editDemo()" href='#'>Edit</a>
问题是当我点击链接时,它没有进入编辑页面。 (以下是观察结果)
- 控制台没有错误
- 我可以看到地址栏中的 url 正在改变,但它很快 返回首页
- 如果我在地址栏中输入
http://localhost/#/app/edit/1,它会起作用 - 它调用
editDemo()方法。
controller.js
$scope.editDemo = function(){
// options I tried
// $location.path( '/edit/1' );
// $location.path( '#/app/edit/1');
// $location.path( 'edit/1');
// $location.url( '/edit/1' );
// $location.url( '#/app/edit/1' );
// $location.url( 'edit/1');
// location.href = '/#/app/edit/1';
}
任何帮助将不胜感激。
【问题讨论】:
-
$location.path( '/edit/1' );应该是正确的,尝试将$event传递给ng-click回调并调用$event.preventDefault() -
@Rasalom,感谢您的快速回答,我将链接更新为 div
<div ng-click="editDemo()">Edit</div>并将代码更新为$location.path( '/edit/1' );,但仍然无法正常工作
标签: angularjs ionic-framework angularjs-routing