Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. <nav class="navbar navbar-default navbar-fixed-top" ng-controller="HeadController">
  2. <div class = "container-fluid">
  3. <ul class="nav navbar-nav navbar-right">
  4. <li><a href="#">{{user.name}}</a></li>
  5. </ul>
  6. </div>
  7. </nav>
  8. <div class="container" ng-view></div>
  9.  
  10. var app = angular.module('app',['ngRoute')
  11. .service("userService", function () {
  12. this.user = {};
  13. this.getUser = function () {
  14. return this.user;
  15. };
  16. this.setUser = function (user) {
  17. this.user = user;
  18. };
  19. })
  20. .service("userService", function () {
  21. this.user = {};
  22. this.getUser = function () {
  23. return this.user;
  24. };
  25. this.setUser = function (user) {
  26. this.user = {
  27. user: user
  28. };
  29. };
  30. })
  31. .config(['$routeProvider', function ($routeProvider) {
  32. $routeProvider
  33. .when('/login', {
  34. templateUrl: 'partials/login.html',
  35. controller: 'LoginCtrl as login'
  36. })
  37. .when('/user/profile',{
  38. templateUrl: 'partials/profile.html,
  39. controller: 'ProfileCtrl as profile'
  40. })
  41. .otherwise({
  42. redirectTo: '/login'
  43. });
  44. }]);
  45. app.controller("HeadController", [
  46. '$scope',
  47. 'userService',
  48. function ($scope, userService) {
  49. $scope.user = userService.getUser();
  50. }]);
  51. app.controller("LoginCtrl", [
  52. "$location",
  53. "userFactory",
  54. "userService",
  55. function ($location, userFactory, userService) {
  56. var login = this;
  57. login.user = {
  58. username: '',
  59. password: '',
  60. isRemember: true
  61. };
  62. login.login = function () {
  63. userFactory.login(login.user)
  64. .success(function (user) {
  65. userService.setUser(user);
  66. $location.path("/user/profile");
  67. })
  68. .error(function (err) {
  69. console.log(err);
  70. });
  71. };
  72. }]);
  73.  
  74. function login(user) {
  75. return $http({
  76. url: '/api/login/',
  77. method: "POST",
  78. data: user,
  79. headers: {
  80. 'Content-Type': 'application/json'
  81. }
  82. });
  83. }
  84. return{login:login};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement