简体   繁体   English

单击按钮时的AngularJS路由不起作用

[英]Angularjs route on button click is not working

I have been trying to make a angularjs application where I want to route to a different html page on a button click. 我一直在尝试制作一个angularjs应用程序,我希望在单击按钮时将其路由到另一个html页面。 But is not working for some unknown reasons. 但是由于某些未知原因而无法正常工作。

My html code 我的html代码

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
     <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular-route.js"></script>
     <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

<body ng-controller="Controller">
<h1>Plunkr Example</h1>
  <button ng-click="changeview()">ClickMe</button>
  <h1>MainPage</h1>
</body>
</html>

My Code 我的密码

var app = angular.module("app", ['ngRoute'])

app.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);

app.config(['$routeProvider',
  function ($routeProvider) {
      $routeProvider.
        when('/Details', {
            templateUrl: 'details.html'
        }).
        when('/Main', {
             templateUrl: 'main.html'
         }).
        otherwise({
            redirectTo: '/Main'
        });
  }]);

app.controller("Controller", function($scope,$routeParams,$location){
  //$scope.ProductId = $routeParams.id;
  $scope.changeview = function () {
        console.log('in function changeview');
        $location.path('/Details');
    }
})

Here is my plunkr 这是我的朋克

Please help. 请帮忙。

You have missed to add the ng-view directive. 您错过了添加ng-view指令的机会。 It needs to be included for the routing to be able to rendered the templates 需要包含它以便路由能够呈现模板

<div ng-view></div>

Working plunker 工作

You need to have ng-view directive in index.html 您需要在index.html中具有ng-view指令

 <div class="viewWrapper">
        <div ng-view></div>
</div>

WORKING DEMO 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM