Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <nav class="navbar navbar-default navbar-fixed-top" ng-controller="HeadController">
- <div class = "container-fluid">
- <ul class="nav navbar-nav navbar-right">
- <li><a href="#">{{user.name}}</a></li>
- </ul>
- </div>
- </nav>
- <div class="container" ng-view></div>
- var app = angular.module('app',['ngRoute')
- .service("userService", function () {
- this.user = {};
- this.getUser = function () {
- return this.user;
- };
- this.setUser = function (user) {
- this.user = user;
- };
- })
- .service("userService", function () {
- this.user = {};
- this.getUser = function () {
- return this.user;
- };
- this.setUser = function (user) {
- this.user = {
- user: user
- };
- };
- })
- .config(['$routeProvider', function ($routeProvider) {
- $routeProvider
- .when('/login', {
- templateUrl: 'partials/login.html',
- controller: 'LoginCtrl as login'
- })
- .when('/user/profile',{
- templateUrl: 'partials/profile.html,
- controller: 'ProfileCtrl as profile'
- })
- .otherwise({
- redirectTo: '/login'
- });
- }]);
- app.controller("HeadController", [
- '$scope',
- 'userService',
- function ($scope, userService) {
- $scope.user = userService.getUser();
- }]);
- app.controller("LoginCtrl", [
- "$location",
- "userFactory",
- "userService",
- function ($location, userFactory, userService) {
- var login = this;
- login.user = {
- username: '',
- password: '',
- isRemember: true
- };
- login.login = function () {
- userFactory.login(login.user)
- .success(function (user) {
- userService.setUser(user);
- $location.path("/user/profile");
- })
- .error(function (err) {
- console.log(err);
- });
- };
- }]);
- function login(user) {
- return $http({
- url: '/api/login/',
- method: "POST",
- data: user,
- headers: {
- 'Content-Type': 'application/json'
- }
- });
- }
- return{login:login};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement