0

ui.routerAngular App の状態変化を検出するために使用しています。しかし、コントローラーはロード時に呼び出されていません。

以下は私のapp.jsコードです。

 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私の状態が の場合、 は呼び出されませんlogin

4

1 に答える 1

1

を使用する$locationProvider.html5Mode(true);場合は、ベースの href の場所を指定する必要があります。状態解決 URL を解析し、http://localhost:8001/admin/#/login問題を解決する必要があります。

メイン インデックス ファイルのセクションに追加<base href="/" />して、アプリケーションのベース パスを指定してみてください。<head />

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

また.htaccess、特定の要求を常にメール インデックス ページにリダイレクトするために、サーバーにファイルを用意します。構造に合わせて少し調整する必要があるかもしれません

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

私のApacheサーバーでmod_rewriteを有効にする必要があることに注意してください。デフォルトでは無効になっていました。

于 2017-01-31T09:58:05.910 に答える