我在AngularJS路由和控制器中遇到问题.这是代码:
的index.html
<!DOCTYPE html> <html ng-app="contacts"> <head> <script src="libs/angular.min%20(1).js"></script> <script src="contacts.js"></script> <script src="index.js"></script> <title></title> </head> <body > <div data-ng-view=""></div> </body> </html>
index.js
var myApp = angular.module('contacts',[]); myApp.config(function ($routeProvider) { $routeProvider .when('/',{ controller: 'ContactsCtrl',templateUrl: '/views/show-contacts.html' }) //.when('/view2',{ controller: 'MyCont',templateUrl: 'V2.htm' }) .otherwise({ redirectTo: '/' }); });
contacts.js
var myApp = angular.module('contacts',[]); myApp.controller('ContactsCtrl',function ($scope) { $scope.name = "omar"; });
但我收到这个错误:
Argument ‘ContactsCtrl’ is not a function,got undefined
有帮助吗?
解决方法
您正在index.js中重新定义您的应用程序,因此在contacts.js中创建的控制器将丢失.从index.js中删除此行:
var myApp = angular.module('contacts',[]);