2015-06-19 107 views
0

在我的網站上,如果我加載模板然後刷新該頁面,則會出現「頁面未找到」錯誤。有什麼辦法可以防止這種情況發生?AngularJS模板重新加載和頁面消失

我可以發佈代碼,但我不太確定哪一段代碼會有價值。這裏是我的主頁(模板被拉入):

<!DOCTYPE html> 
<html data-ng-app="MainApp"> 
    <head > 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <link rel="shortcut icon" type="image/x-icon" href="img/computer.ico" /> 
     <title>Erica Peharda</title> 
     <!--angular uses this base element which path to use when it gets any front end resource- this is the root b/c of "/"--> 
     <base href='/'> 
    </head> 

    <body> 
     <div class='container1'> 
      <div class='page-header'> 
      <nav class="navbar navbar-default"> 
       <div class="container"> 
        <div class="navbar-header"> 
         <a class="navbar-brand" href="/">Welcome to EP Web Developing!</a> 
        </div> 

        <!--default menu bar below--> 
        <ul class="nav navbar-nav navbar-right"> 
         <li><a href="/"><i class="fa fa-home"></i>Home</a></li> 
         <li><a href="/about"><i class="fa fa-shield"></i>About Me</a></li> 
         <li><a href="/contact"><i class="fa fa-comment"></i>Contact for a Quote</a></li> 
         <li><a href="/projects"><i class="fa fa-comment"></i>Projects</a></li> 
        </ul> 
       </div> 
      </nav> 


       <h1 class="titleText pull-left">Erica Peharda</h1> 
       <!--got code below from mycountdown.org--> 
       <div class="countdownContainer pull-right"> 
        <noscript> 
         <div align="center" class="noCountdown"> 
         </div> 
        </noscript> 
        <script type="text/javascript" src="http://mycalendar.org/calendar.php?cp3_Hex=2D00F9&cp2_Hex=F4EAF9&cp1_Hex=0E0E0F&ham=0&img=&hbg=0&hfg=1&sid=0&fwdt=150&text1=Halloween is on 31st October 2015&group=Holiday&calendar=International&widget_number=3"> 
        </script> 
       </div> 
       <div class="photobanner"> 
        <img class="first" src="img/pic1.jpg"/> 
        <img src="img/pic2.jpg"/> 
        <img src="img/pic3.jpg"/> 
        <img src="img/pic4.jpg"/> 
        <img src="img/pic1.jpg"/> 
        <img src="img/pic2.jpg"/> 
        <img src="img/pic3.jpg"/> 
       </div> 
       <div class="row"> 
        <div class="col-md-4"> 
         <a href="https://twitter.com/epeharda"><img class="twitter" src="img/twitter.png"/></a> <a href="https://www.facebook.com/epeharda"><img class="twitter" src="img/facebook.png"/></a> 
        </div> 


       </div> 

      </div> 
        <!--the place holder to render our view dependent on the route--> 
      <div class="row"> 
       <div class="col-sm-12" ng-view> 


       </div> 
      </div> 
     </div> 

     <script type='text/javascript' src="lib/jquery/dist/jquery.min.js"></script> 
       <link rel="stylesheet" type="text/css" href="lib/bootstrap/dist/css/bootstrap.min.css"> 
     <script type="text/javascript" src="lib/bootstrap/dist/js/bootstrap.min.js"></script> 

     <script type="text/javascript" src="lib/angular/angular.min.js"></script> 
     <script type="text/javascript" src="lib/angular-route/angular-route.min.js"></script> 
     <script type="text/javascript" src="lib/angular-resource/angular-resource.min.js"></script> 
     <script type="text/javascript" src="src/app.js"></script> 
     <script type="text/javascript" src="src/controller.js"></script> 
     <script type="text/javascript" src="src/factories.js"></script> 
     <script type="text/javascript" src="src/filter.js"></script> 
     <script type="text/javascript" src="src/style.js"></script> 
     <link rel="stylesheet" type="text/css" href="css/style.css"> 
     <link href='http://fonts.googleapis.com/css?family=Asap' rel='stylesheet' type='text/css'> 

    </body> 
</html> 

在此先感謝!

這裏是我的路由器配置: angular.module( 'MainApp',[ 'ngRoute', 'ngResource'])

.config(function($routeProvider, $locationProvider){ 
    $routeProvider 
     .when('/',{ 
      templateUrl:'views/home.html', 
      contorller: 'HomeController' 
     }) 
     .when('/about', { 
      templateUrl: 'views/about.html', 
      controller: 'AboutController' 
     }) 
     .when('/contact',{ 
      templateUrl: 'views/contact.html', 
      controller: 'EmailController' 
     }) 
     .when('/projects',{ 
      templateUrl: 'views/projects.html', 
      controller: 'ProjectController' 
     }) 
     .otherwise({ redirectTo: '/' }); 
     //this is incase we need to add the #! 
    $locationProvider.html5Mode(
    true); 
}); 
+0

404是不是真的與angular/js相關,但是與您的服務器有關......確保頁面正確地被正確地服務? – timeiscoffee

回答

3

後,我終於縮小我需要搜索的內容,我發現這個解決方案:

Still getting 'Not Found' when manually refreshing with angular.js route

它有讓HTML5做模式。你需要用下面的代碼編輯.htaccess文件,以獲取頁面刷新重新路由到您的index.html文件:

<IfModule mod_rewrite.c> 
Options +FollowSymlinks 
RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !^/$ 
RewriteRule ^.*$ index.html 
</IfModule> 

這是頁面上的最後反應(不知道爲什麼它沒有得到更多的讚揚 - 這是徹底搜索後唯一的工作)。

謝謝大家!

+0

這是一個非常有趣的問題。感謝您發佈您的答案! –

+0

今天我遇到了與ui路由完全相同的問題。我的問題是我的路線在他們之前缺少斜線'/',比如'url:「/ countries」'。所以當我刷新頁面時,它播放了空白頁面。愚蠢的錯誤,但如果這有助於任何人。 – Deke

0

你能提供你的路由器的配置?它可能發生在html5模式下,如果你已經配置它。

+0

我剛更新了代碼。我確實有html5模式。你認爲這是問題嗎?我不知道爲什麼它會在我的本地主機上運行,​​但不是當我將它上傳到我的託管站點(ipage.com)時。 – epurdy

0

我還補充說這可能是服務器端。嘗試在Chrome中打開JavaScript控制檯,然後轉到網絡標籤。 (或在這方面的任何瀏覽器)。看看每一行,找到返回404錯誤的紅線。查看請求url,從那裏你將能夠解決這個問題,如果a)該請求的url是錯誤的或b)該網址沒有任何東西存在。

Here's some info on the debugging within the network tab in Chrome。我建議,因爲它會在很長時間後解決你的問題!

+0

我這樣做,看到它正在指向正確的網站。當它在另一個頁面上時,它並沒有獲得我輸入的角度路線。我一直在使用ipage支持來回發送郵件(我用它們來託管),這是他們的迴應: – epurdy

+0

「我能夠複製與網站http://epeharda.com/的問題。不僅'Contact for一個報價'鏈接,在刷新加載'頁面未找到'錯誤後,本網站的所有鏈接。 這不是我們服務器發出的問題。問題似乎是本網站的腳本。請更正您的端腳本或聯繫web開發人員關於這個問題,我們對此造成的不便表示歉意。「 – epurdy

+0

現在看起來它與html5設置和.htaccess文件有關。這是一個網站,我得到了很多很好的信息:http:// ericduran。io/2013/05/31/angular-html5Mode-with-yeoman /它仍然無法正常工作,但當我有解決方案時我會更新。感謝大家! – epurdy

0

您需要將服務器配置爲針對所有text/html 404錯誤返回/index.html。另一種方法是將/index.html複製爲404.html(或任何404文檔)。

+0

謝謝,我會聯繫iPage,看看我能否完成。我正在考慮切換託管網站 - 你認爲這可以解決問題嗎?這是我使用的第一個也是唯一的託管站點,所以我想知道Angular是否有更好的託管站點。 – epurdy

+0

如果你對服務器有所瞭解,我會選擇數字海洋。您可以獲得root訪問權限,並且可以根據自己的喜好構建服務器。每月5美元。 –