0

I am running into trouble understanding how you can correctly pass parameters using AngularJS.

This is the code I was trying to use in my app.js file for the nested views, however, the nest state never properly renders.

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('news', {
    url: '/news',
    templateUrl: 'templates/news.html',
    controller: 'NewsCtrl'
  })

  .state('news.id', {
    url: '/news/:id',
    templateUrl: 'templates/news.id.html',
    controller: 'NewsCtrl'
  });

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/news');
})

It will try and change the url to #/news/news/:id versus just #/news/:id.

And if I try and change the path to just be the #/news/:id, then the pages do not render correctly.

What is the best approach to achieve these nested views with parameters?

1
  • news.html is the layout or listing page? Commented Jul 10, 2018 at 2:45

1 Answer 1

2

According to ui-route wiki:

When using url routing together with nested states the default behavior is for child states to append their url to the urls of each of its parent states.

If you want to have absolute url matching, then you need to prefix your url string with a special symbol '^'.

So in your case, you should try

 .state('news.id', {
    url: '^/news/:id',
    templateUrl: 'templates/news.id.html',
    controller: 'NewsCtrl'
  });
Sign up to request clarification or add additional context in comments.

Comments

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.