0

I am trying to display view by using dynamic routing. For this I am using ":"

notation. Once data comes back from the server, $location.path('/postuser/'+permalink) is used to redirect to the view. But for some reason I am getting 404 error.

I am using Html 5 mode. I think this the issue.

$location.path('/postuser/'+permalink) sends request to the server:

http://www.learntest/partials/userspostpage.html.

instead of this

http://www.learntest/postuser/somedata.

Here is the router sample

test.config(['$routeProvider', function($routeProvider) {

$routeProvider.when('/welcome', {templateUrl: 'partials/welcome.html', controller: 'welcomeController'});

$routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'singupController'});
$routeProvider.when('/signup', {templateUrl: 'partials/signup.html', controller: 'singupController'});
$routeProvider.when('/', {templateUrl: 'partials/home.html', controller: 'homeController'});
$routeProvider.when('/create', {templateUrl: 'partials/new.html', controller: 'CrudController'});
$routeProvider.when('/user/:permalink', {templateUrl: 'partials/userspostpage.html', controller: 'CrudController'});

$routeProvider.otherwise({redirectTo: '/'});

}]);

 test.config(['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode(true);

}]);

AngularJS files and folder structure

enter image description here

Also in back end I am using ExpressJS router

ExpressJS router

app.get("/postuser/:permalink", daataN);

if I remove /:permalink from the $routeProvider I am able to get the response. But I want url to be dynamic

Please let me know what is the issue here

4
  • I did not understood your comment, daataN is function which return json data Commented Sep 20, 2013 at 14:05
  • Are you using html5mode with angular? Commented Sep 20, 2013 at 14:33
  • Yes I am using html5mode Commented Sep 20, 2013 at 14:56
  • guys help me I struck Commented Sep 24, 2013 at 15:55

1 Answer 1

1

My guess, since you say you're using html5mode is that the server is handling the route before the client gets a chance, thus you will always trigger the GET action on the server before the angular router has a chance to deal with the request.

I think you have a couple of options:

  1. Choose a different server side controller name. (i.e. in express app.get('/user/:permalink'...))
  2. Choose a different client side base route. (i.e. in router '/user/:permalink')
  3. Completely diverge the two because it sounds like you are doing something bad when you have a server route and a client route that perfectly match. The client side routes should make sense for users to copy/paste or bookmark. The server side routes should make sense for you to use as an API. It seems unlikely that they would match.
Sign up to request clarification or add additional context in comments.

5 Comments

exactly, Server is handling route before client get chance.
The key thing is that the ENTIRE route needs to change, just not the location of the wildcard definition. Your router needs to be something completely different i.e. '/user/:permalink' as opposed to '/postuser/:permalink'. See the difference?
changed the route $location.path("/user/"+data.permalink). But I am getting following error GET localhost:3030/user/partials/userspostpage.html 404 (Not Found) angular.min.js:99 $get.id angular.min.js:99 n angular.min.js:95 l angular.min.js:94 $get.l.(anonymous function) angular.min.js:96 (anonymous function) angular.min.js:82 j.promise.then.h angular.min.js:78 j.promise.then.h angular.min.js:78 (anonymous function) angular.min.js:78 $get.e.$eval angular.min.js:88 $get.e.$digest angular.min.js:86 $get.e.$apply angular.min.js:88 e angular.min.js:95 p angular.min.js:98
Please update your original post with the current state of the router as well as the locations of your templates, etc.
I have added current state of router and location of templates

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.