javascript - Angularjs路由模板不显示

标签 javascript angularjs angular-ui-router

我有以下 app.js 文件:

'use strict';

var app = angular.module('app', [
    'auth0',
    'angular-storage',
    'angular-jwt',
    'ui.router',
    'Environment',
    'Api',
    'Profile'

]);


app.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('main', {
            url: '/main',
            templateUrl: 'js/modules/App/views/frontpage.html'
        })
        .state('login', {
            url: '/login',
            templateUrl: 'js/modules/User/views/login.html',
            controller: 'LoginCtrl'
        });

    $urlRouterProvider
        .otherwise('/main');
}]);


app.config(['authProvider', '$httpProvider', '$locationProvider', 'jwtInterceptorProvider',
    function myAppConfig(authProvider, $httpProvider, $locationProvider, jwtInterceptorProvider) {
        authProvider.init({
            domain: 'marcrasmussen.eu.auth0.com',
            clientID: 'hphpe4JiceMW8FSA02CN7yOYl5fUaULe',
            loginUrl: '/login'
        });

        authProvider.on('loginSuccess', ['$location', 'profilePromise', 'idToken', 'store',
            function ($location, profilePromise, idToken, store) {

                console.log("Login Success");
                profilePromise.then(function (profile) {
                    store.set('profile', profile);
                    store.set('token', idToken);
                });

                $location.path('/');
            }]);

//Called when login fails
        authProvider.on('loginFailure', function () {
            alert("Error");
        });

        //Angular HTTP Interceptor function
        jwtInterceptorProvider.tokenGetter = ['store', function (store) {
            return store.get('token');
        }];
//Push interceptor function to $httpProvider's interceptors
        $httpProvider.interceptors.push('jwtInterceptor');

    }]);

app.run(['auth', function (auth) {
    // This hooks all auth events to check everything as soon as the app starts
    auth.hookEvents();
}]);

我有以下 profile.js 文件:

angular.module('Profile', [])
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('profile', {
            abstract: true,
            url: '/profile'
        })
        .state('profile.index', {
            url: '/index',
            templateUrl: 'js/modules/Profile/views/viewProfile.html'
        })
}]);

在我的 index.html 中,文件列出如下:

<script src="js/modules/Profile/lib/profile.js"></script>
<script src="js/modules/App/lib/app.js"></script>
<script src="js/modules/App/directives/login/login.js"></script>

最后,当然我有我的视口(viewport):

<div class="main" ui-view>

</div>

正如您所见,我的应用程序在路由 /main 上启动,这工作得很好,并且 frontpage.html 正在使用所有 html 进行渲染> 在该文件中。

但是,当我转到 profile.index/profile/index 时,控制台中没有显示错误,并且模板文件 js/modules 中没有 html显示/Profile/views/viewProfile.html

谁能告诉我为什么会这样?我做错了什么?

最佳答案

我认为问题可能是你的抽象状态。您没有为此状态定义 templatetemplateUrl。另请注意,抽象状态的模板必须包含 ui-view 指令才能填充其子级。

https://github.com/angular-ui/ui-router/wiki/nested-states-%26-nested-views#abstract-state-usage-examples

您可能需要执行以下操作:

    .state('profile', {
        abstract: true,
        url: '/profile',
        template: '<ui-view />
    })

关于javascript - Angularjs路由模板不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39353629/

相关文章:

javascript - 无法让 .focus 处理动态创建的输入元素

php - 与 MySQL 的连接速度更快?

javascript - jQuery 元素移除

angularjs - Protractor:测试窗口滚动行为

asp.net-mvc - 如何使用 Angular 路由来嵌套 3 个 mvc Razor 模型

javascript - Web 应用程序中的全局变量 VS localStorage 和内存问题

angularjs 1.6.0(现在最新)路线不起作用

css - 使用动画根据内容调整元素大小

javascript - 避免在 Angular ui-router 中使用 ui-view

javascript - 在哪里捕获运行语句中的 $location.search() 变量?为了重建国家