当按钮点击时,AngularJs代码导航到另一个页面

时间:2018-02-26 08:53:09

标签: html angularjs

您好我是AngularJs的新手,当我点击按钮时,我试图从一个页面导航到另一个页面,我按照下面的代码查看我的要求,但它不起作用。

Html文件--->

的index.html,main.html中,london.html,paries.html,others.html

Js文件 - >

AngularFile.js,Others.js

我在london.html文件上保留了按钮。

当我录制它时,我想从london.html文件导航到others.html文件,但我在哪里犯了错误?

有人可以帮我吗?

index.html的: -

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>

<head>
<script src="angularFile.js"></script>
<script type="others.js"></script>
</head>

<body ng-app="myApp">

<p><a href="#/!main">main</a></p>
<a href="#!london">City 1</a>
<a href="#!paris">City 2</a>

<p>Click on the links.</p>

<p>Note that each "view" has its own controller which each gives the "msg" variable a value.</p>

<div ng-view></div>

</body>
</html>

london.html:

   {{msg}}

<input type="Button" ng-click="changeMe()" value="Go to other"/>
others.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Wel come to Others page</h1>
</body>
</html>

angularFile.js

var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider

    .when("/main", {
        templateUrl : "main.htm",
    })
    .when("/london", {
        templateUrl : "london.htm",
        controller : "londonCtrl"
    })
    .when("/paris", {
        templateUrl : "paris.htm",
        controller : "parisCtrl"
    });
});

others.js:

     app.controller("londonCtrl", function ($scope,$location) {
        $scope.msg = "I love London";
        $scope.changeMe = function(){
          $location.path('/others')
        };
    })

;

0 个答案:

没有答案