AngularJS路由包括多个JS文件和目录
AngularJS Routing including multiple JS files and directories
我是 Angular 的新手,路由有问题,我设置得很简单,但仍然无法正常工作。
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css" />
</head>
<body ng-app="app">
<div>{{1+1}}
<ng-view>Loading...</ng-view>
</div>
<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/angular-resource/angular-resource.min.js"></script>
<script type="text/javascript" src="bower_components/angular-route/angular-route.js"></script>
<script type="text/javascript" src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="components/billing/billing.js"></script>
<script>
</script>
</body>
</html>
app.js:
'use strict';
var app = angular.module('app', [
'ngRoute',
'billing'
]);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/billing', {templateUrl: 'components/billing/billing.html', controller: BillingCtrl})
.otherwise({redirectTo: '/billing'});
}]);
components/billing/billing.js:
'use strict';
var billing = angular.module('billing', []);
billing.controller('BillingCtrl', [function($scope) {
var model = {
user: "Adam",
items: [{ action: "Buy Flowers", done: false },
{ action: "Get Shoes", done: false },
{ action: "Collect Tickets", done: true },
{ action: "Call Joe", done: false }]
};
$scope.todo = model;
}]);
components/billing/billing.html:
<div>Hello world {{todo.user}}</div>
根据我的阅读,它应该在 ng-view 中包含 billing.html,因为它是默认路由。但我得到的只是 'Loading...'.
非常感谢任何帮助!
当您为路由指定控制器时,它应该是控制器的名称(作为字符串),而不是对控制器函数的引用。
$routeProvider
.when('/billing', {
templateUrl: 'components/billing/billing.html',
controller: "BillingCtrl"
})
(注意 BillingCtrl
周围的引号)
我是 Angular 的新手,路由有问题,我设置得很简单,但仍然无法正常工作。
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css" />
</head>
<body ng-app="app">
<div>{{1+1}}
<ng-view>Loading...</ng-view>
</div>
<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/angular-resource/angular-resource.min.js"></script>
<script type="text/javascript" src="bower_components/angular-route/angular-route.js"></script>
<script type="text/javascript" src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="components/billing/billing.js"></script>
<script>
</script>
</body>
</html>
app.js:
'use strict';
var app = angular.module('app', [
'ngRoute',
'billing'
]);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/billing', {templateUrl: 'components/billing/billing.html', controller: BillingCtrl})
.otherwise({redirectTo: '/billing'});
}]);
components/billing/billing.js:
'use strict';
var billing = angular.module('billing', []);
billing.controller('BillingCtrl', [function($scope) {
var model = {
user: "Adam",
items: [{ action: "Buy Flowers", done: false },
{ action: "Get Shoes", done: false },
{ action: "Collect Tickets", done: true },
{ action: "Call Joe", done: false }]
};
$scope.todo = model;
}]);
components/billing/billing.html:
<div>Hello world {{todo.user}}</div>
根据我的阅读,它应该在 ng-view 中包含 billing.html,因为它是默认路由。但我得到的只是 'Loading...'.
非常感谢任何帮助!
当您为路由指定控制器时,它应该是控制器的名称(作为字符串),而不是对控制器函数的引用。
$routeProvider
.when('/billing', {
templateUrl: 'components/billing/billing.html',
controller: "BillingCtrl"
})
(注意 BillingCtrl
周围的引号)