2017-01-30 8 views
0

ui.router을 사용하여 각도 앱의 상태 변화를 감지했습니다. 그러나 컨트롤러가로드시 호출되지 않습니다.ui.router 상태가 예상대로 작동하지 않습니다.

아래 코드는 내 app.js 코드입니다. 내 상태가 login

var admin = angular.module('admin',['admin.core']); 
    angular.module('admin.core', ['ui.router', 'satellizer', 'ngMaterial']); 

    admin.config(function($mdThemingProvider, $stateProvider, 
          $urlRouterProvider, $authProvider, $locationProvider){ 
    $mdThemingProvider.theme('default').primaryPalette('light-blue', { 
     'default': '700', 
     'hue-1' : 'A200', 
    }).accentPalette('blue'); 

    // Satellizer configuration that specifies which API route the JWT should be retrieved from 
    $authProvider.loginUrl = '/api/authenticate'; 

    // Redirect to the auth state if any other states are requested other than users 
    $urlRouterProvider.otherwise('/admin/login'); 
    console.log($stateProvider) 
    $stateProvider 
     .state('login', { 
      url: '/admin/login', 
      name: 'login', 
      controller: 'AuthController as auth', 
     }) 
     .state('users', { 
      url: '/admin/users', 
      controller: 'UserController as user' 
     }); 
     $locationProvider.html5Mode(true); 
    }); 

    admin.controller('AuthController', AuthController); 

    function AuthController($auth, $state, $scope) { 
    console.log("Called"); 
    var vm = this; 

    vm.login = function() { 
     var credentials = { 
      email: vm.email, 
      password: vm.password 
     } 
     console.log(credentials); 
     // Use Satellizer's $auth service to login 
     $auth.login(credentials).then(function(data) { 
      // If login is successful, redirect to the users state 
      $state.go('users', {}); 
     }); 
    } 

    } 

AuthController가 호출되지 않습니다.

+0

왜 '사용자'가 아닌 '사용자'입니까? 그리고 이름을 추가하십시오 : 'users' –

+0

@ PiotrPęczek .. – Gags

+0

당신은 루트 앱 html 어딘가에'ui-view'를 가지고 있습니까? – devqon

답변

1

$locationProvider.html5Mode(true);을 사용하는 경우 기본 href 위치를 입력해야합니다. 귀하의 주정부 해결 URL을 http://localhost:8001/admin/#/login으로 구문 분석하여 문제를 해결해야합니다.

주 색인 파일의 <head /> 섹션에 <base href="/" />을 추가하여 응용 프로그램의 기본 경로를 지정하십시오.

https://docs.angularjs.org/api/ng/provider/$locationProvider

또한 메일 인덱스 페이지에 특정 요청을 리디렉션에 allways하기 위해 서버에 .htaccess 파일을 제공합니다. 은 구조에 맞게 약간 조정해야합니다. mod_rewrite에 내 아파치 서버에서 사용할 수 있다고

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteRule ^index.html$ - [L] 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule . /index.html [L] 
</IfModule> 

참고. 기본값으로 비활성화되었습니다.

+0

내가 admin에 있으므로 Base URL은 '이 될 것입니다. 그리고 그것은 처음에만 작동합니다. 만약'http : // localhost : 8001/admin'을 방문하면 stateprovider가'http : // localhost : 8001/admin/login'을 만들었지 만 지금 새로 고침을 사용한다면 컨트롤러가 호출되지 않습니다 ... – Gags

+0

이렇게하면 초기 문제가 해결됩니까? 이제 새로운 문제가 'F5'/ 페이지 새로 고침시 발생합니까? 옳은? –

+0

'.htaccess'를 서버에 추가 했습니까? 위의 업데이트 된 답변보기 –