从控制器中调用 angularjs ui-router 状态 [英] Calling an angularjs ui-router state from within a controller

查看:14
本文介绍了从控制器中调用 angularjs ui-router 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 angularjs ui-router 在应用中建立各种状态.虽然我知道我可以从 html 中的链接(通过 ui-sref)进入状态,但是否可以从控制器内进入状态?

I use the angularjs ui-router to establish the various states within an app. While I know I can go to the state from a link in the html (via ui-sref), is it possible to go to the state from within a controller?

下面的代码片段是一个简单的 angularjs 应用程序,可以帮助说明我的观点.

The code snippets below are a simple angularjs app to help illustrate my point.

在下面的例子中,我有两种状态:

In the example below, I have two states:

  1. 有一个名为 home 的状态,它是一个简单的表单,包含一个文本输入字段和一个调用控制器中的搜索功能的按钮.
  2. 有一个名为 search 的状态接受名为 text 的查询参数.它的控制器调用一个基于文本执行搜索的服务.结果显示在页面上.
  1. There is a state called home that is a simple form containing a text input field and a button that calls a search function in the controller.
  2. There is a state called search that takes a query parameter called text. Its controller calls a service that performs a search based on the text. The results are displayed to the page.

首先是模块的启动.

var app = angular.module('example', [
    'ui.router'
  ]);

下面是前面解释的ui-routing状态的配置.

Below is the configuration of the ui-routing states as explained earlier.

app.config(
  function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/');
    $stateProvider.
      state('home', {
        url: '/',
        template: '<input ng-model="text" type="text" placeholder="Query Text">' +
          '<button type="button" ng-click="search()">Search</button>',
        controller: 'SearchFormController'
      }).
      state('search', {
        url: '/search?text',
        template: '<pre>{{results | json}}</pre>',
        controller: 'SearchController'
      });
  });

SearchFormController 控制搜索的表单输入.这个控制器只是将表单输入转发到 search 状态.是否可以引用 search 状态而不是构造 URL 并调用 $location.path?

The SearchFormController controls the form input of search. This controller just forwards the form input to the search state. Is it possible to reference the search state as opposed to constructing a URL and calling $location.path?

app.controller('SearchFormController', 
  function($scope, $location) {
    $scope.text = '';
    $scope.search = function() {
      // Can I use the 'search' state here instead of 
      // constructing a url and calling $location.path?
      $location.path('/search?text='+ encodeURIComponent($scope.text));
    };
  });

搜索控制器 SearchController 如下所示.它需要 stateParams(即查询参数)来发出 $http 调用.

The controller of the search, SearchController, would look something like below. It would take the stateParams (i.e., query parameters) to issue an $http call.

app.controller('SearchController',
  function($scope, $stateParams, $http) {
    $scope.results = [];
    asUrl = function(resourceUrl, queryParams) {
      // do stuff and return http url
    };
    $http.get(asUrl("/someServiceUrl/search", $stateParams))
      .success(function(data, status) {
        $scope.results = data;
      })
      .error(function(data, status) {
        // display an error message
      });
  });

同样,在 SearchFormController 中,是否可以通过名称从配置中引用 search 状态?例如,在 html 页面中,我可以有这样的链接:<a ui-sref="search({text:Foo})">Search where text=Foo</a>其中 search 状态由名称引用并传入参数.可以从控制器调用类似的东西(按名称,传入参数)吗?

Again, in the SearchFormController, is it possible to reference the search state from the config by name? For example, in an html page I could have a link like this: <a ui-sref="search({text:Foo})">Search where text=Foo</a> where the search state is referenced by name and passes in the parameters. Can something similar be invoked from a controller (by name, passing in parameters)?

推荐答案

是的,查看文档:http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state对于 $state.go(stateName, params, options)

Yes, check the doc: http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state for $state.go(stateName, params, options)

转换到新状态的便捷方法.$state.go 在内部调用 $state.transitionTo 但自动将选项设置为 { location: true, inherit: true, relative: $state.$current, notify: true }.这使您可以轻松地使用绝对路径或相对路径,并仅指定您想要更新的参数(同时让未指定的参数从当前活动的祖先状态继承).

Convenience method for transitioning to a new state. $state.go calls $state.transitionTo internally but automatically sets options to { location: true, inherit: true, relative: $state.$current, notify: true }. This allows you to easily use an absolute or relative to path and specify only the parameters you'd like to update (while letting unspecified parameters inherit from the currently active ancestor states).

我们可以使用许多设置,但所有设置都在上面的文档链接中明确定义

There are many settings we can use, but that all is clearly defined in the doc linke above

也可能很有趣:

AngularJS UI-Router中ui-sref和$state.go的区别

这篇关于从控制器中调用 angularjs ui-router 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆