Skip to content

Commit 4f66e94

Browse files
committed
AngularJS router fixes
Signed-off-by: Allann Jones
1 parent 71e2e04 commit 4f66e94

File tree

14 files changed

+70
-54
lines changed

14 files changed

+70
-54
lines changed

java-servlet-angularjs/src/main/webapp/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
<script src="js/angular-ui-router.js"></script>
1212
<script src="js/ng-table.js"></script>
1313
<script src="js/app.js"></script>
14+
<script src="js/main_controller.js"></script>
15+
<script src="js/user_login_controller.js"></script>
1416
<script src="js/bank_new_controller.js"></script>
1517
<script src="js/bank_list_controller.js"></script>
1618
</head>
1719

1820
<body ng-app="app">
1921
<div class="menu">
22+
<a ui-sref="main" ui-sref-active="active">Home</a> |
23+
<a ui-sref="user_login" ui-sref-active="active">Login</a> |
2024
<a ui-sref="bank_list" ui-sref-active="active">Banks</a> |
2125
<a ui-sref="bank_new" ui-sref-active="active">New bank</a>
2226
</div>
2327

24-
<div class="page" ui-view=""></div>
28+
<div class="page" ui-view="" ng-controller="indexCtrl"></div>
2529
</body>
2630
</html>
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
var app = angular.module('app', ['ui.router', 'ngTable']);
22

33
app.config(function($stateProvider) {
4+
var mainState = {
5+
name: 'main',
6+
url: '/',
7+
templateUrl: '/java-servlet/pages/index.html'
8+
};
9+
410
var helloState = {
511
name: 'bank_list',
612
url: '/bank/list',
7-
templateUrl: '/java-servlet/bank/list/index.html'
13+
templateUrl: '/java-servlet/pages/bank/list/index.html'
814
};
915

1016
var aboutState = {
1117
name: 'bank_new',
1218
url: '/bank/new',
13-
templateUrl: '/java-servlet/bank/new/index.html'
19+
templateUrl: '/java-servlet/pages/bank/new/index.html'
20+
};
21+
22+
var userLoginState = {
23+
name: 'user_login',
24+
url: '/user/login',
25+
templateUrl: '/java-servlet/pages/user/login/index.html'
1426
};
1527

28+
$stateProvider.state(mainState);
1629
$stateProvider.state(helloState);
1730
$stateProvider.state(aboutState);
31+
$stateProvider.state(userLoginState);
32+
});
33+
34+
app.controller('indexCtrl', function($scope, $state) {
35+
console.log('Index controller');
36+
37+
//$state.go('main');
1838
});

java-servlet-angularjs/src/main/webapp/js/bank_new_controller.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
app.controller('bankNewCtrl', function($scope, $http) {
1+
app.controller('bankNewCtrl', function($scope, $http, $state) {
22
$scope.status = '';
33

44
$scope.bankSave = function() {
@@ -26,9 +26,11 @@ app.controller('bankNewCtrl', function($scope, $http) {
2626
console.log('message: ' + obj.message);
2727

2828
if (obj.status == 0) {
29-
$scope.status = 'Bank is registrated.';
29+
$scope.status = 'Bank is registered.';
3030

3131
$scope.name = '';
32+
33+
$state.go('bank_list');
3234
}
3335
else {
3436
$scope.status = 'Error on bank registration.';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
app.controller('mainCtrl', function($scope, $state) {
2+
console.log('Main controller');
3+
4+
$state.go('main');
5+
});

java-servlet-angularjs/src/main/webapp/js/user_login_controller.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
app.controller('userLoginCtrl', function($scope, $http) {
1+
app.controller('userLoginCtrl', function($scope, $http, $state) {
22
$scope.status = '';
33

44
$scope.login = function() {
@@ -28,6 +28,10 @@ app.controller('userLoginCtrl', function($scope, $http) {
2828

2929
if (obj.status == 0) {
3030
$scope.status = 'User is authenticated.';
31+
32+
console.log('Going to main page');
33+
34+
$state.go("main");
3135
}
3236
else {
3337
$scope.status = 'Error on user authentication.';

java-servlet-angularjs/src/main/webapp/bank/list/index.html renamed to java-servlet-angularjs/src/main/webapp/pages/bank/list/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<div ng-controller="bankListCtrl">
2+
<h1>Banks</h1>
3+
24
<table ng-table="tableBanks" class="table" show-filter="true">
35
<tr ng-repeat="bank in $data">
46
<td title="'ID'" filter="{ id: 'number'}" sortable="'id'">

java-servlet-angularjs/src/main/webapp/bank/new/index.html renamed to java-servlet-angularjs/src/main/webapp/pages/bank/new/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<div ng-controller="bankNewCtrl">
2+
<h1>New bank</h1>
3+
24
<form name="loginForm">
35
<p>
46
Name: <input type="text" ng-model="name"/>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div ng-controller="mainCtrl">
2+
<h1>Main</h1>
3+
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<h1>User account information</h1>
3+
</div>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<div ng-controller="userLoginCtrl">
2+
<h1>Login</h1>
3+
4+
<form name="loginForm">
5+
<p>
6+
Username: <input type="text" ng-model="username" />
7+
</p>
8+
<p>
9+
Password: <input type="password" ng-model="password" />
10+
</p>
11+
<p>
12+
<button ng-click="login()">Login</button>
13+
</p>
14+
</form>
15+
<span ng-bind="status"></span>
16+
</div>

0 commit comments

Comments
 (0)