2017-10-21 23 views
0
var app = angular.module('MyApp', ['ngRoute']); 
app.config(function ($routeProvider) { 
    $routeProvider 
     .when("/Home", { 
      templateUrl: "Home/EmployeeList", 
      controller: "listController", 
     }) 
     .when("/Home1", { 
      templateUrl: "Home/EmployeeTable", 
      controller: "tableController", 
     }) 
     .otherwise({ 
      redirectTo: "/Home/Index" 
     }) 
     .controller("listController", function ($scope) { 
      $scope.message = "In list controller"; 
     }) 
}); 

为什么在运行代码TypeError:routeProvider.when(...)。when(...)。否则(...)。控制器错误显示在cosole中。为什么providerRoute不工作

+0

请提供完整的错误信息。 –

+0

Uncaught Error:[$ injector:modulerr] http://errors.angularjs.org/1.6.4/$injector/modulerr?p0=MyApp&p1=TypeError%3A%20%24routeProvider.when(...).when( ...)。3) at angular.js:38 at angular.js:4920 at q(angular.js:403) at g(angular.js:4880) at eb(angular.js:4802 ) 在C(angular.js:1914) 在钪(angular.js:1935) 在UE(angular.js:1820) 在angular.js:33367 在HTMLDocument.b(angular.js:3431) –

回答

0

将控制器注册呼叫移出配置块。没有$routeProvider.controller

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

app.config(function ($routeProvider) { 
    $routeProvider 
     .when("/Home", { 
      templateUrl: "Home/EmployeeList", 
      controller: "listController", 
     }) 
     .when("/Home1", { 
      templateUrl: "Home/EmployeeTable", 
      controller: "tableController", 
     }) 
     .otherwise({ 
      redirectTo: "/Home/Index" 
     }) 

}); 


app.controller("listController", function ($scope) { 
    $scope.message = "In list controller"; 
})