简体   繁体   English

Angular JS UI路由器共享控制器

[英]Angular js ui-router share controller

I want to give 2 parts of my UI the same controller but still let them have each of their own unique controllers. 我想为UI的2个部分提供相同的控制器,但仍然让它们拥有各自的唯一控制器。

  $stateProvider
    .state('standard.page', {
      url: '/:page',
      resolve: {
        page: function($stateParams) {
          ...
        },
      },
      views: {
        'content': {
          templateUrl: '/tmpl/page',
          controller: 'controllercontent'
        },
        'sideMenu': {
          templateUrl: '/tmpl/menu',
          controller: 'controllermenu',
        }
      }
    })

So I want both content and sideMenu to share a controller. 因此,我希望content和sideMenu共享一个控制器。 If I add a controller above the views then it requires a new template, I want to use the standard template instead of making a unique template for this state. 如果我在视图上方添加一个控制器,则它需要一个新模板,我想使用standard模板,而不是为此状态创建唯一的模板。 Any ideas how I can get 3 controllers going in this example? 在这个示例中,我如何获得3个控制器的任何想法? Thanks. 谢谢。

I battled with this at some point in time, and I believe I made a template file that isn't directly accessible (via abstract: true). 我在某个时候对此进行了斗争,我相信我制作了一个不能直接访问的模板文件(通过abstract:true)。 Here's an example... 这是一个例子

.state('standard', {
    url: '/standard',
    abstract: true,
    templateUrl: '/tmpl/standard.html',
    controller: 'SharedController'
    },
})

.state('standard.page', {
    url: '/:page',
    resolve: {
    page: function($stateParams) {
            ...
        },
    },
    views: {
        'content': {
                templateUrl: '/tmpl/page',
                controller: 'controllercontent'
            },
            'sideMenu': {
                templateUrl: '/tmpl/menu',
                controller: 'controllermenu',
            }
        }
});

In your tmpl/standard.html file, make sure this exists somewhere within the file: 在您的tmpl / standard.html文件中,确保该文件存在于文件中的某个位置:

<div ui-view="sideMenu">
<div ui-view="content">

Hope this points you in the right direction. 希望这能为您指明正确的方向。

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

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