无法使用laravel 5和angularjs加载任何模板

时间:2017-06-06 23:42:00

标签: angularjs laravel-5 angular-ui-router

我有一个问题是将某些模板加载到" /" index laravel 5 route with angularjs利用$stateProvider$routeProvider或任何地方。

index.blade.php的内容很好,每次我直接在网页浏览器上访问模板的所有内容都很好。

问题是使用angularjs加载。开发人员控制台上出现此错误:

Error: [$compile:tpload] Failed to load template: /templates/site/home (HTTP status: undefined undefined)

所以,我使用{path?}Route::any()来定制我的laravel路线。

我使用templates作为前缀,但实际上并不存在。这只是要使用的laravel路线的虚拟URL。

enter image description here

此实现如下:

web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
| Here is where you can register web routes for your application.
| These routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
*/

Route::group(['prefix' => 'panel'], function () {

    Route::any('{path?}', function () {
        return view('panel/index');
    })->where('path', '.+');

});

Route::group(['prefix' => 'templates'], function () {

    Route::any('{path?}', function ($name) {

        dd($name);

        return view($name);
    })->where('path', '.+');

});

Route::group(['prefix' => ''], function () {

    Route::any('{path?}', function () {
        return view('site/index');
    })->where('path', '.+');

});

app.js

var panelTplDir = '/templates/panel';
var siteTplDir = '/templates/site';


/***********************************
 * Routes of website without panel -
 * ---------------------------------
 *
 * @type {{name: string, url: string, templateUrl: string, controller: string}}
 */

// Routes to out panel.
var siteIndexRoute = {
    name: 'index',
    url: '/',
    templateUrl: siteTplDir + '/home',
    controller: 'IndexCtrl as index'
};
var notfoundRoute = {
    name: 'notfound',
    url: '/not_found',
    templateUrl: siteTplDir + '/notfound',
    controller: 'SiteCtrl as site',
    params: {
        route: null,
        paramSearch: null
    }
};

// Routes of authentication.
var authRoute = {
    name: 'login',
    url: '/login',
    templateUrl: siteTplDir + '/auth/login',
    controller: 'AuthController as auth'
};
var registRoute = {
    name: 'register',
    url: '/register',
    templateUrl: siteTplDir + '/auth/register',
    controller: 'AuthController as auth'
};


/**********************************
 * Routes of administration panel -
 * --------------------------------
 *
 * @type {{name: string, url: string, templateUrl: string, controller: string}}
 */

// Routes of dashboard.
var homeRoute = {
    name: 'panelIndex',
    url: '/panel/dashboard',
    templateUrl: panelTplDir + '/home',
    controller: 'DashboardCtrl as home'
};

// Routes of user.
var userMeRoute = {
    name: 'me',
    url: '/panel/me',
    templateUrl: panelTplDir + '/users/user_info',
    controller: 'DashboardCtrl as user'
};
var usersRoute = {
    name: 'userList',
    url: '/panel/users',
    templateUrl: panelTplDir + '/users/get_users',
    controller: 'DashboardCtrl as user'
};

// Routes of stores.
var storeIndexRoute = {
    name: 'storeIndex',
    url: '/panel/stores',
    templateUrl: panelTplDir + '/stores/index',
    controller: 'StoresController as store'
};

所以......

我不会发现panel路线上的模板发生了什么。

enter image description here

在这里,一切都很好。

enter image description here

在这里,一切都很好。

enter image description here

在这里,一切都很好。

enter image description here

更新

幸运的是,我在Angular ui.bootstrap failing to load templates

找到了解决方案

经过一些测试后,我意识到HTTP请求拦截器已停止$ templateCache正常工作。

在拦截器中添加一个异常(以便它会忽略&#34;请求&#34;转到模板缓存)之后一切正常。

拦截器代码:

request: function (config) {
config.headers = config.headers || {};

if (config.url.startsWith("/templates/")) {
    //Not modifying requests to these urls, 
    //as they are angular template cache requests
    return config;
}

//Do the interceptor work here. Add headers or whatever.
return config;

0 个答案:

没有答案