I want to develop a site with main page that route to several other pages. All page will have their own app.js
file with their own .controller
for easy code management.
pages.html
<html ng-app="myPageApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-route.js"></script>
<script src="js/mypages.js"></script>
</head>
<body>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><i class="fa fa-home"></i> Home</a></li>
<li><a href="#users"><i class="fa fa-comment"></i> Users</a></li>
</ul>
<!-- angular templating -->
<!-- this is where content will be injected -->
<div ng-view></div>
</body>
</html>
mypages.js
var myPageApp = angular.module("myPageApp",['ngRoute']);
myPageApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/users.html',
});
});
users.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="../js/myusers.js"></script>
</head>
<body>
<div ng-app="myUsersApp">
<div ng-controller="UsersCtrl">
<ul class="unstyled">
<li ng-repeat="user in UsersCtrl.users">
<span >{{user}}</span>
</li>
</ul>
<input type="text" ng-model="user" placeholder="Name please">
User {{user}}
</div>
</div>
</body>
</html>
myusers.js
var myUsersApp = angular.module("myUsersApp",['']);
myUsersApp.controller('UsersCtrl',[ '$scope', function($scope){
$scope.users = ['john','marry'];
$scope.add = function() {
$scope.users.push($scope.user);
$scope.user = "";
}
}]);
I can never get the myusers.js
to work. when I Inspect element
, It seems like myusers.js
was not called at all. Any thoughts?
ng-app
andng-controller
without arguments; Second, you can only useng-app
once per page. Why you are trying to make these controllers into completely separate apps isn't exactly clear, but whatever the reason, this isn't the way to do it. There is enough wrong here that it would be hard to offer one answer that corrects all the issues.js
files?js
file. But as the project grow the controller become bigger and harder to manage. That is why I want to have it in separatejs
files.