AngularJS在新页面中显示1个帖子

时间:2017-03-15 12:15:41

标签: javascript html angularjs

当用户点击Test Post 1

中的titleDetails.html时,我想在下一个html页Test Post 1中仅显示index.html

1)index.html中的titleDetails()

<a ng-click="titleDetails(post)">{{ post.title }} </a>

2)控制器变量和titleDetails()方法:

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 titleDetails(post)
{ 
    $scope.postDetail = post; 
    $location.path('#/titleDetails'); 
}

3)titleDetails.html中的正文:

<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>

完整代码:

app.js

(function () {
    angular
    .module("BlogApp", [])
    .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'); 
    }

    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);
        }
    }
})();

的index.html

<!DOCTYPE html>
<html lang="en" ng-app="BlogApp">
<head>
    <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>

titleDetails.html:

<!DOCTYPE html>
<html lang="en" ng-app="BlogApp">
<head>
    <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>

enter image description here

2 个答案:

答案 0 :(得分:1)

BlogController中的新范围变量

$scope.postDetail = null;

和方法titleDetails()就像

$scope.titleDetails = function(post) {
$scope.postDetail = post;
}

并在index.html中进行更改

 <a  ng-click="titleDetails(post)">{{ post.title }} </a>

答案 1 :(得分:1)

希望以下步骤适合您 将发布对象传递给titleDetail()函数。
index.html

<a  ng-click="titleDetails(post)">{{ post.title }} </a>

在功能中将帖子分配给$ rootScope变量

$scope.titleDetails = function(post) {
   $rootScope.postDetail = post;
}

现在您的 titleDetail.html 页面控制器:

$scope.post = $rootScope.postDetail;