AngularJS:ng视图中的控制器
触发ng路由时,控制器负载出现问题。 这是我的主页:AngularJS:ng视图中的控制器,angularjs,angularjs-controller,ng-view,angularjs-ng-route,angular1.6,Angularjs,Angularjs Controller,Ng View,Angularjs Ng Route,Angular1.6,触发ng路由时,控制器负载出现问题。 这是我的主页: <!DOCTYPE html> <html> <head ng-app="testapp"> <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="angular-route.js">
<!DOCTYPE html>
<html>
<head ng-app="testapp">
<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript" src="angular-route.js"></script>
<script type="text/javascript" src="app.js"></script>
<title>XX</title>
</head>
<body>
<nav ng-controller="defaultnav"></nav>
<div ng-view></div>
</body>
</html>
在page1.html I inisiate控制器内,如下所示:
<div ng-controller="page1">
</div>
<script type="text/javascript">
app.controller('page1', function ($scope) {
// code...
})
</script>
我不知道处理这个问题的最佳做法。当我编写这个代码时,我得到了一个错误[error,ctrlreg],它说我在注册控制器方面有问题。
请给我一些建议来解决这个问题。
提前谢谢
在JS中,var app=angular.module'enterprise',['ngRoute']行中的应用程序名称;是企业
在HTML中,应用程序名为testapp。改为
单独加载java脚本是最好的做法。因此,从html中删除js代码,并将其保存在单独的文件中并加载
用于plnkr中的工作演示
您可以使用AngularJS ngRoute:
// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', ['ngRoute']);
// configure our routes
scotchApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/home', {
templateUrl: 'pages/home.html',
controller: 'mainController'
})
// route for the about page
.when('/about', {
templateUrl: 'pages/about.html',
controller: 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl: 'pages/contact.html',
controller: 'contactController'
})
//otherwise redirect to home
.otherwise({
redirectTo: "/home"
});
});
// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {
// create a message to display in our view
$scope.message = 'Everyone come and see how good I look!!!!';
});
scotchApp.controller('aboutController', function($scope) {
$scope.message = 'Look! I am an about page.';
});
scotchApp.controller('contactController', function($scope) {
$scope.message = 'Contact us!.';
});
在angular应用程序启动后,您无法向注册控制器。Tou在应用程序引导之前需要这样做。ng app=testapp,模块名称为enterprise?除了Manish所说的:应该在路由定义中指定控制器,而不是在视图中使用ng控制器。哦,对不起,我编辑了这个。如果我添加。当/page1,{templateUrl:page1.html,controller:page1}?它也不工作
// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', ['ngRoute']);
// configure our routes
scotchApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/home', {
templateUrl: 'pages/home.html',
controller: 'mainController'
})
// route for the about page
.when('/about', {
templateUrl: 'pages/about.html',
controller: 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl: 'pages/contact.html',
controller: 'contactController'
})
//otherwise redirect to home
.otherwise({
redirectTo: "/home"
});
});
// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {
// create a message to display in our view
$scope.message = 'Everyone come and see how good I look!!!!';
});
scotchApp.controller('aboutController', function($scope) {
$scope.message = 'Look! I am an about page.';
});
scotchApp.controller('contactController', function($scope) {
$scope.message = 'Contact us!.';
});