0

I'm building an app and i have a little problem.

For the index, i change my url with history.pushstate when i click on a button with jquery. For example, www.mysite.com become www.mysite.com/something without reloading the page. But if a load this page i don't find www.mysite.com because "something" doesn't exist.

And I don't know how do the link between them.

I tried to do this but it doesn't work with AngularJS.

var myApp = angular.module('photo', ['ngRoute']);


myApp.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'machin.html'
    });
    $routeProvider.when('/something-one', {
        templateUrl: 'machin.html'
    });
    $routeProvider.when('/something-two', {
        templateUrl: 'machin.html'
    });
    $routeProvider.otherwise({ redirectTo: '/' });
}]);

This is the way to do it or I'm just lost ? ^^


Solved

My solution works sorry all. I forgot the script angular-route but the rest of my script works.

1
  • You can use $route.current = "/url" Commented Apr 21, 2015 at 13:01

2 Answers 2

1

In your button, you can bind event using ng-click instead of using jQuery.

<button ng-click="goto('/something')">Go to something</button>

In your controller:

$scope.goto = function(url) {
   $location.path(url);
}

When you're working with Jquery, you're out of angular scope. So angular will not know about the URL change. You may need to trigger digest cycle in that case.

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

3 Comments

Yes thanks but i need "something" to be referencied and with location.path it's after the hash. I don't want the reload of the page so i asking myself if it's possible.
No it's not possible. Because it's not there in your routes and so angular will redirect to default route /
My solution works :D sorry for your lost of time and thanks a lot. I forgot to include angular-route to my html.
0

You could try this

var myApp = angular.module('photo', ['ngRoute']);
myApp.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'machin.html'
    })
    .when('/something-one', {
        templateUrl: 'machin.html'
    })
    .when('/something-two', {
        templateUrl: 'machin.html'
    })
    .otherwise({ redirectTo: '/' });
}]);

and in your html

<a href="#/something-one">Go to something one</a>

1 Comment

Thanks for the answer, but i my solution worked too i did a little mistake ^^ Thanks for your time.

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.