Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Routing for nested states in ui-router

So I am using UI-Router to build my AngularJS app. However I am confused as to how the URL Routing works in case of nested states mainly due to the conflicting ideas (as per my understanding) provided in the wiki for UI-Router.

The first idea

idea 1 Hence maybe abstract state is used below

idea 1

The second idea

idea 2

As given in the documentation (first idea), url of parent state is prepended to that of it's children only in case of 'abstract':true property defined on the parent state.

However the documentation (second idea) also mentions how the above is a default feature.

Aren't the two ideas above conflicting for the same concept ? Or have I completely misunderstood them ?

like image 982
Navin Nagpal Avatar asked Sep 29 '15 10:09

Navin Nagpal


People also ask

What is UI view in AngularJS?

The ui-view directive tells angularJS where to inject the templates your states refer to. When a state is activated, its templates are automatically inserted into the ui-view of its parent state's template. If it's a top-level state—which 'business' is because it has no parent state–then its parent template is index.

What is $state in AngularJS?

$stateProvider is used to define different states of one route. You can give the state a name, different controller, different view without having to use a direct href to a route. There are different methods that use the concept of $stateprovider in AngularJS.

What is state based routing?

State-Based Routing is a type of Geographic Routing that automatically directs your callers to the most appropriate store or office in their state or territory when they call your 1300/1800/13 or Local Virtual number.

What is UI routing?

UI-Router is the defacto standard for routing in AngularJS. Influenced by the core angular router $route and the Ember Router, UI-Router has become the standard choice for routing non-trivial apps in AngularJS (1. x).


1 Answers

Well, the documentation statements are correct. Maybe not clear for someone - but correct. It simply says:

1) No inheritance of url: "..url..." setting. It means, that child state won't have the url set with the same value as parent. Both values are independent.

2) There is implicit url concatenation. Child state url (in address bar, not the setting) is built from all its ancestors url.

So, the documentation is correct. This example is just for play ... It shows what we know. Child has different url setting then parent. Child state url in address bar is build from its url setting - prefixed with parent(s) url

// NON abstract
  .state('parent1', {
      abstract: false,
      url: "/parent1",
      templateUrl: 'tpl.html',
  })
  .state('parent1.child1', { 
      url: "/child1",
      templateUrl: 'tpl.html',
  })
// abstract
  .state('parent2', {
      abstract: true,
      url: "/parent2",
      templateUrl: 'tpl.html',
  })
  .state('parent2.child2', { 
      url: "/child2",
      templateUrl: 'tpl.html',
  })

url in href:

non abstract
<a href="#/parent1">
<a href="#/parent1/child1">
abstract
<a href="#/parent2"> - cannot navigate there - is abstract
<a href="#/parent2/child2">
like image 193
Radim Köhler Avatar answered Nov 15 '22 08:11

Radim Köhler