Skip to content

Deep Linking and State Transitions #200

Closed
@gwright

Description

@gwright

I'm trying to better understand how to effectively organize my code around the state transitions model presented by ui-router. I'm also new to Angular so please feel free to point out my mistakes.

It seems as though there are two general sources of state transitions:

  • explicit calls to transitionTo(), often triggered by controller functions due to UI interaction
  • implicit calls via changes to the URL either via landing on a deep link or end-user edits of the URL

By default, $stateProvider configuration takes care of updating the URL and changing the view to match the new state but any other application logic has to be handled separately.

It would make sense to put all that logic in the controller but I'm having a hard time understanding how to arrange for those implicit state transitions to trigger a controller function.

onEnter and onExit would seem to be the obvious 'hooks' but I'm not sure how to provide the correct $scope to those callbacks, they aren't injectable functions.

So what is the recommended way to react to state transitions triggered by deep links and/or URL edits by the user? Should I be putting all that logic in the controller or should it be somewhere else?

Activity

nateabele

nateabele commented on Jun 21, 2013

@nateabele
Contributor

Controllers can use $scope's $on() method to listen for events fired by state transitions. Hopefully that answers your question. Generally, controllers are instantiated on an as-needed basis, when their corresponding scopes are created, i.e. when the user manually navigates to a state via a URL, $stateProvider will load the correct template into the view, then bind the controller to the template's scope, so any initialization upon entry into the state happens that way.

@timkindberg this breakdown is probably ripe for the dev guide.

gwright

gwright commented on Jun 22, 2013

@gwright
Author

Thanks @nateabele for pointing me in the right direction. The $stateChangeSuccess event was the piece I was missing in understanding how to unify state transition processing.

akarelas

akarelas commented on Jul 13, 2013

@akarelas

I didn't understand: If a browser arrived at a deep link, how do we change the state to the correct one, if it's not done automatically for us?

nateabele

nateabele commented on Jul 14, 2013

@nateabele
Contributor

@akarelas It should be done automatically.

akarelas

akarelas commented on Jul 14, 2013

@akarelas

Ok but it's not done automatically when $locationProvider.html5Mode is true. For more info see #250

timkindberg

timkindberg commented on Jul 14, 2013

@timkindberg
Contributor

html5mode assumes some server side redirects are set up.

akarelas

akarelas commented on Jul 14, 2013

@akarelas

Those are perfectly set up on my example website

timkindberg

timkindberg commented on Jul 14, 2013

@timkindberg
Contributor

So they all redirect back to index.html?

nateabele

nateabele commented on Jul 14, 2013

@nateabele
Contributor

@akarelas Not sure what to tell you. It works fine for me. Is there a reason you have the app set up in a <script /> block in the middle of the page?

akarelas

akarelas commented on Jul 14, 2013

@akarelas

I'm using Firefox on Ubuntu Linux. Let me try with Chrome. And yes, they all redirect back to index.html.

akarelas

akarelas commented on Jul 14, 2013

@akarelas

@nateabele Are you saying that if you directly click on this url, you see text in the black & red frame? http://gist1.perlmodules.net:8080/a/funny

I get no text (other than the links underneath the empty black frame) on neither Firefox nor Chrome

akarelas

akarelas commented on Jul 14, 2013

@akarelas

I wrote my app in a <script /> block to avoid writing a new file which might confuse you. But this shouldn't be the problem.

nateabele

nateabele commented on Jul 14, 2013

@nateabele
Contributor

@nateabele Are you saying that if you directly click on this url, you see text in the black & red frame?

Sorry for not being clear. I'm saying that the initial state transition based on a deep-linked URL works fine for me in my own apps. Seems to work fine for everyone else, as well.

While I don't have time to debug your setup for you, I'm sure if you went over it carefully, you'd find the issue.

akarelas

akarelas commented on Jul 14, 2013

@akarelas

I went through it carefully, in fact this is a minimum site that reproduces the error, which I wrote for two purposes:

  • reproduce the bug for you
  • see if I did something wrong the first time around

I reproduced the bug for you, without detecting anything wrong in my program

9 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nateabele@gwright@timkindberg@akarelas@laurelnaiad

        Issue actions

          Deep Linking and State Transitions · Issue #200 · angular-ui/ui-router