0

I am building a web Single Page Application using AngularJS. I need that clicking on link change URI in client browser without http request.

http://example.com/ ---> it shows my single page application and clicking on a specific link I need the URL is http://example.com/about but without send http request and show hidden div.

5
  • It'll always need to send an HTTP request to get the template. Commented Nov 2, 2014 at 21:43
  • The template is loaded but it is hidden. I know it is posible but I can't remember how do it. Commented Nov 2, 2014 at 21:49
  • docs.angularjs.org/api/ng/provider/$locationProvider, docs.angularjs.org/api/ng/directive/script Commented Nov 2, 2014 at 22:31
  • ng-hide and ng-show..? Commented Nov 2, 2014 at 22:33
  • Noah, yes, but changing url Commented Nov 3, 2014 at 11:15

2 Answers 2

1

I don't know what you precisely want to do but if you only want do one http request you can perhaps use angular ui router with something like

$stateProvider
.state('main', {
  url: "/",
  templateUrl: "main.html"
})
.state('about', {
  url: "/about",
  templateUrl: "main.html",
  controller: function($scope) {
    $scope.showDiv = "true";
  }
})

That way you can switch state and because everything you need is already loaded, nothing gets loaded anymore. Or perhaps you can also use parameters.

But why is it so bad to have one additional request? That would be something interesting to know! :)

EDIT: The easy approach with $location (https://docs.angularjs.org/guide/$location)

index.html:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example</title>
    <base href="/">
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.1/angular.min.js"></script>
    <script src="app.js"></script>
</head>
<body ng-app="html5-mode">
    <div ng-controller="LocationController">
        <button ng-click="changeUrl()">Change url</button>
    </div>
</body>
</html>

app.js:

angular.module('html5-mode', []) 
.controller("LocationController", function ($scope, $location) {
    $scope.$location = {};
    $scope.changeUrl = function () {
        // https://docs.angularjs.org/guide/$location
        console.log("The current path: " + $location.path());
        console.log("Changing url...");
        $location.path('/newValue')
    };
})
.config(function ($locationProvider) {
    $locationProvider.html5Mode(true).hashPrefix('!');
})

Be sure to set the basePath correct.

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

1 Comment

I only need change url without http request.
0

Take a look at html2js. This is a grunt task to convert your html templates into a pre-cached js file.

Ideally you would run this as part of your build process. As well you can run a watch task to compile your HTML templates into the pre-cache whenever you save a template -- this is nice for development.

If you are already using gulp, there is a package for you. There are many alternatives to html2js that do essentially the same thing. So if it doesn't suit your needs, try another.

So with this in place, when you navigate to another page -- the HTML template will just be pulled out of angular's cache, and not grabbed from the server.

2 Comments

I only need change url without http request.
Yep. That is what html2js is for. So if you are using uiRouter (or ngRouter) and HTML5 pushstate, you will be able to navigate through your app without making any HTTP requests for pages.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.