ui-router:没有视图模板的路由

vca*_*llo 26 angularjs angular-ui-router

是否可以设置ui-router只有控制器的路线?目的是在某个URL上,我唯一想做的就是以编程方式执行操作,而不是根据视图显示任何内容.我已经阅读了文档,但我不确定他们是否提供了这样做的方法.

是的,我读过这个:https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-open-a-dialogmodal-at-a-certain-state ,但是这不是我想要的.

例如,我只想说我有一个带有视图的基本体:

<body ui-view></body>
Run Code Online (Sandbox Code Playgroud)

还有一些基本的配置:

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

/go/myaction被访问,视图为空.是否有可能做到这一点?

vca*_*llo 9

我能够通过将我正在采取程序化操作的无头状态重定向到无头状态结束时的状态来解决这个问题:

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

  • 它在v0.2.13中不起作用.没有模板,Controller不会实例化 (4认同)

Pet*_*ler 9

没有视图,您不能拥有控制器,但您可以使用onEnter而不是控制器.如果您不想在访问此状态时更改当前视图,则可以将其定义为子状态:

$stateProvider

   // the parent state with a template
   .state('home', {
      url: '/home',
      templateUrl: '/home.html',
      controller: 'HomeCtrl'
   })

   // child of the 'home' state with no view
   .state('home.action', {
      url: '/action',
      onEnter: function() {
         alert('Hi');
      },
   });
Run Code Online (Sandbox Code Playgroud)

现在home.html你可以做这样的事情:

<a href ui-sref=".action">Greet me!</a>
Run Code Online (Sandbox Code Playgroud)