简体   繁体   English

如何在另一个状态内渲染角度ui路由状态?

[英]How to render an angular ui-route state within another state?

I have two ui route states S1 and S2 . 我有两个ui路线状态S1S2 S1 is a query state, having a form by which we query some data. S1是一种查询状态,具有一种查询某些数据的形式。 S2 is the result state in which I am showing that data which I got from S1 state. S2是结果状态,其中显示了我从S1状态获得的数据。

Now I want to provide S1 (query state) within S2 result state so that user can modify there query in S2 state itself. 现在,我想在S2结果状态中提供S1 (查询状态),以便用户可以在S2状态本身中修改查询。

How I can access S1 template and controller in S2 state ? 如何以S2状态访问S1模板和控制器?

Code: 码:

$stateProvider
        .state('form', {
            url: '/form',
            templateUrl: '/modules/form',
            controller: 'formController'
        })
        .state('result', {
            url: '/result',
            templateUrl: '/modules/result',
            controller: 'resultController'
        })

I want to use form state within result state. 我想在result状态中使用form状态。

Note: I am new in angular, please provide me some sugestions for such kind of scenerio's. 注意:我是新手,请提供一些有关此类场景的建议。

This is somewhat of a subjective answer, but having used ui-router quite a lot, I think it's great for handling your various routes but if you want modular reusable parts to use on different pages I'd claim you'll have an much easier time just using angular's native directives instead. 这在某种程度上是一个主观的答案,但是已经使用了ui-router了很多,我认为这对于处理您的各种路由非常有用,但是如果您想在不同页面上使用模块化可重用部件,我想您会容易得多时间只是使用angular的本机指令来代替。

app.directive('someform', function() {
  return {
    restrict: 'E',
    templateUrl: 'form.html',
    controller : 'formController'
  };
});

http://plnkr.co/edit/JynPLQQgE1UqVMBOLvdS?p=preview Plunk for completeness. http://plnkr.co/edit/JynPLQQgE1UqVMBOLvdS?p=preview完整的Plunk。 (You may want to note that ui-router is completely superfluous in this example.) (在此示例中,您可能需要注意ui-router是完全多余的。)

As for communicating between controllers, one way is to use $broadcast , $emit and $on . 至于控制器之间的通信,一种方法是使用$broadcast$emit$on But I much prefer a service that holds your results, and inject it into both controllers for easy access. 但是我更喜欢一种可以保存您的结果并将其注入两个控制器中以便于访问的服务。

app.factory('Results', function(){
  results = [{value:'aaaa'},{value:'bbb'}, {value:'cccc'}];
  return {
    get : function(){
      return results;
    },
    add : function(value){
      results.push(value);
    }
  }
})

http://plnkr.co/edit/IC6OysrLw64lG8ed1sfL?p=preview http://plnkr.co/edit/IC6OysrLw64lG8ed1sfL?p=预览

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM