I am new to the AngularJS world and I am trying to customize the laravel / angular JWT base script after this tutorial .
What I would like to do is use the syntax "Controller As" instead of $scope , as indicated in the tutorial. So far, here is what I have:
app.js
var app = angular.module('app', ['ngRoute']); app.run(function () {}); app.config(function ($routeProvider, $locationProvider) { $routeProvider.when('/', { templateUrl: 'js/templates/login.html', controller: 'LoginController' }); });
input controller
angular.module('app') .controller('LoginController', function ($scope) { this.title='toto'; this.data = {}; this.submit = function () { console.log(this.data); }; });
admin view
<!doctype html> <html lang="en"> <head> <title>Blog Administration</title> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"/> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular-route.js"></script> <script src="js/app.js"></script> <script src="js/controllers/loginController.js"></script> </head> <body ng-app="app"> <div id="wrapper"> <div class="container" id="view" ng-view> </div> </div> </body> </html>
registration template
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body ng-controller="LoginController as login"> <div class="container"> <div id="login" class="col-md-4 col-md-offset-4"> <div id="login-form"> <h4>{{ login.title }}</h4> <form ng-submit="login.submit()"> <div class="form-group"> <input id="username" type="text" ng-model="login.data.username"/> </div> <div class="form-group"> <input id="password" type="password" ng-model="login.data.password"/> </div> <div class="form-group"> <button class="btn btn-primary" type="submit">Login</button> </div> </form> </div> </div> </div> </body> </html>
The problem is that it does nothing. Can I use a controller for each template?
If I put the <ng-controller="LoginController as login"> directive in the view body instead of the template, everything will display correctly.
Is it good to define a controller on one template? Since the login template should be as general as possible, therefore, it can be loaded in any view if the user is not authenticated, so I believe that the login () action in the controlelr processing the administrator view is incorrect.
Can anyone advise me on best practices to have here?