ui-router: A route with no view template

Issue

Is it possible to setup a route in ui-router that only has a controller? The purpose being that at a certain URL, the only thing I’d like to do is take action programatically, and not display anything in terms of a view. I’ve read through the docs, but I’m not sure if they offer a way to do this.

Yes, I have read this: https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-open-a-dialogmodal-at-a-certain-state, but that is not quite what I am looking for.

For example, let’s just say I have a basic body with view:

<body ui-view></body>

And some basic config:

// Routes
$stateProvider
  .state('myaction', {
    url: "/go/myaction",
    onEnter: function() {
      console.log('doing something');
    }
  });

When /go/myaction is visited, the view is blank. Is it possible to do this?

Solution

I was able to solve this problem by redirecting the headless state I was taking programmatic action in, to a state WITH a view at the end of the headless state:

$stateProvider
  .state('myaction', {
    url: "/go/myaction",
    onEnter: function() {
      console.log('doing something');
    }
    controller: function($state) {
      $state.go('home');
    }
  });

Answered By – vcardillo

Answer Checked By – Cary Denson (AngularFixing Admin)

Leave a Reply

Your email address will not be published.