2017-03-18 105 views
0

預期結果:當單擊index.htmlTitletitleDetails.html打開了TitleCommentAngularJS在第二個URL空結果

問題:當點擊Title時,URL更改爲。

但內容保持不變。當我刷新URL時頁面顯示0帖子。

截圖:(的index.html)&(titleDetails.html點擊index.html的標題後) enter image description here

截圖:(當內容保持清爽titleDetails.html後同樣來自index.html的重定向後)

enter image description here

代碼:

1)的index.html

<!DOCTYPE html> 
<html lang="en" ng-app="BlogApp"> 
<head> 
<base href="/" /> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
    <script src="app.js"></script> 
    <title>Title</title> 
</head> 
<body> 
    <div class="container" ng-controller="BlogController"> 
     <h1>Blog</h1> 
      <input ng-model="post.title" class="form-control" placeholder="title"/> 
      <textarea ng-model="post.body" class="form-control" placeholder="body"></textarea> 
      <button ng-click="createPost(post)" class="btn btn-primary btn-block">Post</button> 
      <button ng-click="updatePost(post)" class="btn btn-success btn-block">Update</button> 

      <div ng-repeat="post in posts"> 
       <h2> 
        <a ng-click="titleDetails(post)">{{ post.title }} </a> 
        <a ng-click="editPost(post._id)" class="pull-right"><span class="glyphicon glyphicon-pencil"></span></a> 
        <a ng-click="deletePost(post._id)" class="pull-right"><span class = "glyphicon glyphicon-remove"></span></a> 
       </h2> 
       <em>{{post.posted}}</em> 
       <p>{{post.body}}</p> 
      </div> 
    </div> 
</body> 
</html> 

2)titleDetails.html

<!DOCTYPE html> 
<html lang="en" ng-app="BlogApp"> 
<head> 
<base href="/" /> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
    <script src="app.js"></script> 
    <title>Title</title> 
</head> 
<body> 
    <div class="container" ng-controller="BlogController"> 
     <h1>Blog</h1> 
      <div> 
       <h2> 
        <a>{{ postDetail.title }} </a> 
       </h2> 
        <em>{{postDetail.posted}}</em> 
        <p>{{postDetail.body}}</p> 
      </div> 
    </div> 
</body> 
</html> 

3)app.js

(function() { 
    angular 
    .module("BlogApp", []) 
    .config(function($locationProvider) { 
     $locationProvider.html5Mode(true).hashPrefix('!'); 
    }) 
    .controller("BlogController", BlogController); 

    function BlogController($scope, $http, $rootScope, $location) { 
     $scope.createPost = createPost; 
     $scope.deletePost = deletePost; 
     $scope.editPost = editPost; 
     $scope.updatePost = updatePost; 
     $scope.titleDetails = titleDetails; 
     $scope.postDetail = null; 

    function init() { 
     getAllPosts(); 
    } 
    init(); 

    function titleDetails(post) 
    { 
     $scope.postDetail = post; 
     $location.path('/titleDetails.html'); 
    } 

    function updatePost(post){ 
     console.log(post); 
     $http 
     .put("/api/blogpost/"+post._id, post) 
     .success(getAllPosts); 
    } 

    function editPost(postId){ 
     $http 
     .get("/api/blogpost/"+postId) 
     .success(function(post){ 
      $scope.post = post; 
     }); 
    } 

     function deletePost(postId){ 
      $http 
      .delete("/api/blogpost/"+postId) 
      .success(getAllPosts); 
     } 

     function getAllPosts(){ 
      $http 
      .get("/api/blogpost") 
      .success(function(posts) { 
       $scope.posts = posts; 
      }); 
     } 

     function createPost(post) { 
      console.log(post); 
      $http 
      .post("/api/blogpost",post) 
      .success(getAllPosts); 
     } 
    } 
})(); 
+2

Angualar用於單頁面應用程序開發。通過[角度SPA](https://scotch.io/tutorials/single-page-apps-with-angularjs-routing-and-templating) –

回答

0

AngularJS是一個SPA(單頁面應用程序)導向。您的鏈接仍然與傳統的多頁面導航相關聯。由於$location$http.get不是用於在場景中加載模板和導航的正確服務,因此您將不得不重新使用您的應用。

只要您使用$routeProvider,AngularJS就可以加載模板並相應地更新地址欄。 Scoth.io made a simple tutorial關於如何設置路由。

但是你基本上要包括ngRoute到應用程序:

angular.module('ngRouteExample', ['ngRoute']) 

然後配置你的路由:

.config(function($routeProvider, $locationProvider) { 
    $routeProvider 
    .when('/', { 
     templateUrl: 'blogPosts.html', 
     controller: 'BlogController' 
    }) 
    .when('/post/:id', { 
     templateUrl: 'titleDetails.html', 
     controller: 'TitleController' 
    }); 
} 

因爲答案可能會很長,下面Plunker演示了一個簡單的路由實現供你學習:

https://plnkr.co/edit/twmbG0G9XjOqF82JtyMC?p=preview

相關問題