AngularJS Route 和Controller 分開存至不同的js檔案,Route應該只有一個但是Controller會有很多個,通通放在一個檔案就很整理和看。
這個範例主要有 index.html 然後再Home.html 和 HomeCtrl.js 一組,Alan.html,Bill.html和MemberCtrl.js一組。
先建立Home.html, HomeCtrl.js,Alan.html,Bill.html和MemberCtrl.js
Home.html
<h2>Kenneth's Home<h2>
{{ message }}
HomeCtrl.js
var HomeApp = angular.module('HomeApp',[]);
HomeApp.controller('HomeCtrl',function($scope){
$scope.message='Welcome!! this is Kenneth\'s home';
});
Alan.html
<h2>Alan<h2>
My name is {{ name }}
Bill.html
<h2>Bill<h2>
My name is {{ name }}
MemberCtrl.js
var HomeApp = angular.module('HomeApp',[]);
HomeApp.controller('AlanCtrl',function($scope){
$scope.name='Alan';
});
HomeApp.controller('BillCtrl',function($scope){
$scope.name='Bill';
});
主頁 -- index.html
重點是紅色的字,要注意的是js檔載入的順序controller要先載入最後在Router
<!DOCTYPE html>
<html lang="en">
<head>
<!--
** Todo-Sortable-List Example App
** Licensed under the Apache License v2.0
** http://www.apache.org/licenses/LICENSE-2.0
** Built by Kenneth Hu (Kenneth_hu@hotmail.com)
-->
<title>AngularJS Routing example</title>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
padding-top: 10px;
background-color: #F5F5F5;
}
</style>
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="../../assets/js/html5shiv.js"></script>
<script src="../../assets/js/respond.min.js"></script>
<![endif]-->
</head>
<body ng-app="sampleApp">
<div class="container">
<div class="row">
<div class="col-md-3">
<ul class="nav">
<li><a href="#Home"> Home </a></li>
<li><a href="#Alan"> Alan </a></li>
<li><a href="#Bill"> Bill </a></li>
</ul>
</div>
<div class="col-md-9">
<div ng-view></div>
</div>
</div>
</div><!-- /.container -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="HomeCtrl.js"></script>
<script src="MemberCtrl.js"></script>
<script src="app.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-65211878-2']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>
App.js
要把兩個controller的模組inject進去這樣才能運作,但是
var sampleApp = angular.module('sampleApp', ['HomeApp','MemberApp']);
sampleApp.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/Alan', { templateUrl: 'templates/Alan.html', controller: 'AlanCtrl' }). when('/Bill', { templateUrl: 'templates/Bill.html', controller: 'BillCtrl' }). when('/', { templateUrl: 'home.html', controller: 'HomeCtrl' }). when('/Home', { templateUrl: 'Home.html', controller: 'HomeCtrl' }). otherwise({ redirectTo: '/' }); }]);
如果兩controller的JS檔取一樣的module名稱會如何呢?
答案: 後一個會把前一個蓋掉 ,以我的案例HomeCtrl.js 會被MemberCtrl.js蓋掉會出現如下圖的
留言列表