0

Main page URL: http://localhost:3000/

Current second page URL: http://localhost:3000/#/titleDetails.html

Expected second page URL: http://localhost:3000/titleDetails.html

Currently when I click on the title in my main page, the URL contains an extra /# which causes the page to be redirected to titleDetails.html.

The directory of titleDetails.html and index.html is in the same directory.

May I know how can I fix this?

Original Post: AngularJS Display 1 Post in New Page

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

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>

Console Error in index.html:

angular.js:13708 Error: [$location:nobase] http://errors.angularjs.org/1.5.7/$location/nobase
    at angular.js:38
    at sf.$get (angular.js:13384)
    at Object.invoke (angular.js:4709)
    at angular.js:4508
    at d (angular.js:4655)
    at e (angular.js:4679)
    at Object.invoke (angular.js:4701)
    at R.instance (angular.js:10234)
    at m (angular.js:9147)
    at g (angular.js:8510)

1 Answer 1

3

Angular has 3 routing operates:

  • Hashbang Mode
  • HTML5 Mode
  • Hashbang in HTML5 Mode

You can configure: $locationProvider.html5Mode(true).hashPrefix('!');

Check documentation

Sign up to request clarification or add additional context in comments.

10 Comments

after I put the configuration in my controller variables, my page stop loading any posts
you need add this to angular config, not in controller. Something like this: .config(function($locationProvider) {}
May I know where can I find the angular config file? or is it the top part of the code in my app.js?
You are missing ) inconfig definition, before controller
add <base href="/" /> in your head section
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.