Angular路由不正常工作?

35 浏览
0 Comments

Angular路由不正常工作?

我正在尝试使用angular js进行路由,服务器正常运行。但是angular路由不正常工作,只有main.html页面出现在ng-view中,second.html没有出现。当在html中点击第一个和第二个时,同样的mainController在工作,而不是secondController。

HTML

<!Doctype html>

 

 

 

 

     

main.html

this is main

 

second.html

this is second

 

app.js

var myApp = angular.module('myApp',['ngRoute']);
myApp.config(function ($routeProvider){
$routeProvider
.when('/',{
    templateUrl: 'pages/main.html',
    controller: 'mainController',
})
.when('/second',{
    templateUrl: 'pages/second.html',
    controller: 'secondController',
})
   });
     myApp.controller('mainController',
       ['$scope','$log','$location',function($scope,$log,$location){
        $log.info($location.path());
        console.log("first");
  }]);
   myApp.controller('secondController',
       ['$scope','$log','$location',function($scope,$log,$location){
        $log.info($location.path());
        console.log("second");
  }]);

admin 更改状态以发布 2023年5月21日
0
0 Comments

问题出在你的脚本上,它们不能正常工作。使用来自 googleapis 的脚本。

var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            template: '

Main Page

', controller: 'mainController', }) .when('/second', { template: '

{{ test }}

', controller: 'secondController', }) }); myApp.controller('mainController', ['$scope', '$log', '$location', function ($scope, $log, $location) { $log.info($location.path()); console.log("first"); }]) .controller('secondController', ['$scope', function ($scope) { $scope.test = 'Testing angular routes'; }]);





  • first
  • Second
  • 0