第二个URL中的AngularJS空结果

时间:2017-03-18 11:38:53

标签: javascript html angularjs

预期结果:在Title点击Comment时,在titleDetails.html中打开Titleindex.html

问题:点击http://localhost:3000/titleDetails.html后,网址更改为Title

但内容仍然相同。当我刷新URL时,页面显示0个帖子。

屏幕截图: (index.html)& (在index.html中单击标题后的titleDetails.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);
        }
    }
})();

1 个答案:

答案 0 :(得分:1)

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