2014-09-29 88 views
0

AngularJs代码导航到按钮单击事件上的另一个页面。我已经捕获了按钮点击事件,但无法导航到其他页面。 以下是我的代码: HTML文件:AngularJs代码导航到buttonclick中的另一个页面

<!DOCTYPE html> 

<html> 

<head> 

<script src="js/angular.min.js"></script> 

<script src="js/app/home.js"></script> 

</head> 

<body ng-app="myapp"> 

    <div ng-controller="TestCtrl"> 

     <button ng-click="clicked()">Click me!</button> 

</body> 

</html> 

和JavaScript文件是:

var VLogin = angular.module('myapp',[]); 

VLogin.controller('TestCtrl', ['$scope',function($scope) { 

    $scope.clicked = function(){ 

     $location.path('/test.html'); 
    } 

}]); 
+0

谁能帮我解决我的问题:http://stackoverflow.com/questions/38546218/angularjs-form-is-不提交数据 – Ankit 2016-07-25 02:38:52

回答

8

建议使用$位置服务之前做一些事情尝试像下面所示...

在html中,

<button ng-click="clicked()">Click</button> 

在JS,

$scope.clicked = function(){ 
     window.location = "#/test.html"; 
} 

我希望它可以帮助你......

+0

SDK:是的,它为我工作..但在路径中我必须像这样改变:window.location =“../www/index。 HTML“; – 2014-09-30 05:20:52

+0

不要忘记接受这个答案,如果你觉得它有用:-) – SDK 2014-09-30 11:06:40

+0

嗨,我想按钮重定向到另一个组件的HTML页面。这怎么能做到? – 2017-12-22 07:54:26

3

您可以使用$ location.path( '/ newPageUrl')导航到新的页面。

阅读here了解更多关于$位置的信息。

编辑: 正确的控制器代码将是(增加$位置作为依赖后): -

VLogin.controller('TestCtrl', ['$scope', '$location',function($scope, $location) { 

$scope.clicked = function(){ 

    $location.path('/test.html'); 
} 

}]);

+0

以上是我的代码.. – 2014-09-29 12:04:50

+0

Abhishek Jain:我已经使用$ location.path('/ newPageUrl')代码,但它不适合我。 – 2014-09-29 12:24:38

+0

您必须将$ location服务传递给控制器​​ – Teq1 2014-09-29 12:32:57

0

如果要导航到另一个页面的onclick我建议使用好老的“链接”

<a href="/another-page">Go to another page</a> 

除非你有重定向比Abhishek Jain

+0

Teq1:我想在按钮单击事件功能。 – 2014-09-29 12:27:03

+0

@BibinJose使用我的解决方案,你做:)但正如我所提到的,使用旧的'href',除非你必须在重定向之前做一些事情,而不是使用'$ location'服务,这样更好地测试。 – Teq1 2015-12-08 21:41:53

+0

@ Teq1不能放在按钮的内部或外部 - htmlangular验证失败 – Lars 2016-10-13 13:24:09

相关问题