Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

$state.go not redirecting

im trying to redirect to a specific page with a given id, but is not working, it says that cannot resolve the state.

My url example:

http://localhost/#/app/lab/edit/table/101

My js:

$state.go('app.lab.edit.table',$stateParams.calculationId); $stateParams.calculationId is my id numer (101).

Error Message:

Error: Could not resolve 'app.lab.edit.table' from state 'app.lab.result.existing' StateParams Object:

Object {calculationId: "101", calculation: null, fromSave: false} What im doing wrong?

Steven Parker
Steven Parker
230,879 Points

:x: A URL referencing "localhost" will not work for anyone other than yourself.

1 Answer

Erik Nemesis
Erik Nemesis
13,356 Points

Probably that you are trying to call $state.go on an internal ui-view that is not relative to the state you call state.go in. Let me explain:

From what I see you are in state app.lab.result.existing, which means that your html is something of the sort (handled by $stateProvider.states)

<!-- ui view for "app" -->
<ui-view>
  ...
  <!-- ui view for lab -->
  <ui-view>
      ...
      <!-- ui view for result, controller: ResultController -->
      <ui-view>
           ...
           <!-- ui view for existing, controller: ExistingController -->
           <ui-view>
            This is the content for existing
           </ui-view>
        </ui-view>
    </ui-view>
</ui-view>

However I dont know your code but, if you try to go to app.lab.edit.table from your ExistingController, basically ui router will try to resolve each of the parent states, that is "app", "lab", "edit" and "table". However, from your current point of view the <ui-view> for edit is unavailable, because it is instead the ui-view for result. So ui router cannot switch states.

Let's do an analogy with Object inheritance:

Let's day we have those two inheritance chains Human -> Worker -> CompanyWorker -> Employee and

Human -> Worker -> AssociationWorker -> Volunteer

Obviously Employee and Volunteer are subclasses of Worker, but you cannot cast Volunteer to Employee and vice-versa.

This is what happens here.

Hope that helps