2017-05-06 57 views
1

我正在使用flask + Angular + MongoDb應用程序。我正在使用狀態提供程序。狀態從應用程序加載完美。但是當我刷新頁面時,它正在拋出404錯誤。Python燒瓶角度應用程序狀態不刷新

  1. App.py:(燒瓶)
application = Flask(__name__) 
    @application.route('/') 
    def showMachineList(): 
     print ("Inside the show machine list function") 
     return application.make_response(open('templates/index.html').read()) 
  • App.js(角)
  • angular.module('testapp', ['ngCookies', 'ngResource', 
         'ngSanitize', 'ui.router', 'ui.bootstrap','ngMaterial','ngCookies' 
        ]) 
        .config(function($urlRouterProvider, $locationProvider,$interpolateProvider) { 
         $urlRouterProvider.otherwise('/'); 
         //$interpolateProvider.startSymbol('//').endSymbol('//'); 
         $locationProvider.html5Mode(true).hashPrefix('!'); 
        }); 
    
  • 國家
  • angular.module('testapp') 
        .config(function ($stateProvider) { 
        console.log("inside create outage request") 
        $stateProvider 
         .state('cor', { 
         url: '/cor', 
         template: '<cor></cor>' 
         //templateUrl: '/static/client/app/acn/acn.html' 
         }); 
        }); 
    

    我已經加入index.html中的<base href="/">爲好。 當我刷新頁面"http://localhost:5000/cor"其投擲404. 你能讓我知道我失蹤了嗎?

    回答

    1

    這與單頁應用相當普遍的問題。看起來您的Angular應用程序可以在客戶端正確處理cor狀態,但您的Web服務器無法知道/cor路徑應該在初始加載時路由到相同的Angular應用程序。

    嘗試添加第二個路線的終點:

    @application.route('/') 
    @application.route('/cor') 
    def showMachineList(): 
        ... 
    

    或者,你可以使用catch-all route處理任何路徑後綴。

    0

    刷新頁面時,瀏覽器將URL「http://localhost:5000/cor」發送到服務器。正如我在代碼中看到的那樣,沒有定義/cor路由,只有/。我認爲你必須定義一個。

    @application.route('/cor') 
    def showMachineList(): 
        with open('templates/index.html') as fd: 
         content = fd.read() 
        return application.make_response(content) 
    

    問題可能是在您的模板:在其中定義鏈接cor之一。你應該有這樣的事情:

    <a href="#cor">link to cor</a> 
    

    我讓你寫了這不是假設:

    <a href="/cor">link to cor</a> 
    
    +0

    但我已經定義了狀態,我認爲它也應該從Angular狀態加載。 –

    +0

    您的HTML模板中可能有一個錯誤。 –