angular-ui/ui-router,如何使用 $stateProvider 注入部分视图? [英] angular-ui/ui-router, how do I inject partial view using $stateProvider?

查看:28
本文介绍了angular-ui/ui-router,如何使用 $stateProvider 注入部分视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试注入一个面板进行编辑,具体取决于查看者是否是所有者.现在,我在将面板/部分视图注入 html 时遇到了麻烦.我希望我的 composition/views/view.html 成为基本"html 页面,其中部分被注入.然后部分视图位于 composition/views/partials/views.tools.html.是否有人看到我的 $stateProvider 有问题,这可以解释为什么我不能将部分注入到我的 views.html 中?

I am trying to inject a panel for editing, depending if the viewer is the owner, or not. Right now, I am having trouble just injecting the panel/partial view into the html. I want my compositions/views/view.html to be the 'base' html page, where the partial is injected in. Then the partial view is at compositions/views/partials/views.tools.html. Does anybody see something wrong with my $stateProvider that would explain why I cannot inject my partial into my views.html?

这是我的 $stateProvider:

Here is my $stateProvider:

$stateProvider
        .state('all compositions', {
            url: '/compositions/recent',
            templateUrl: 'compositions/views/list.html'
        }). 
          state('view', {
                url: '/compositions/view/:compositionId',
                views: {
                'theTool':{
                 templateUrl:'compositions/views/partials/view.tool.html'   ,
                    controller: 'CompositionsController'
                }
                },
                templateUrl: 'compositions/views/view.html',
                controller: 'CompositionsController',

            }). //other states here

这是我的 view.html(主 html)的标记

this is my markup for my view.html (main html)

<div ui-view="theTool"></div>
<section data-ng-controller="CompositionsController" data-ng-init="findOne()">
    <h2>{{composition.title}}</h2>
    <div ng-bind-html="trustedContent"></div>
</section>

非常感谢任何帮助或建议.谢谢

Any help or advice is greatly appreciated. Thanks

推荐答案

在这里我创建了一个 工作示例,它应该给出所有的答案.

Here I created a working example, which should give all the answers.

调整后的状态定义为:

$stateProvider
  .state('allCompositions', {
    url: '/compositions/recent',
    templateUrl: 'compositions.views.list.html'
  }).

state('view', {
  url: '/compositions/view/:compositionId',
  views: {
    '': {
      templateUrl: 'compositions.views.view.html',
      controller: 'CompositionsController',
    },
    'theTool@view': {
      templateUrl: 'compositions.views.partials.view.tool.html',
      controller: 'CompositionsController'
    }
  },

最重要的是,compositions.views.view.html 现在正在扮演 sibling 视图 theTool.两个视图都定义在相同的状态(视图")上,但其中一个被注入到另一个中.

the most important is, that the compositions.views.view.html is now playing the role of a target for the sibling view theTool. both views are defined on the same state ('view') but one of them is injected into the other.

我也在 index.html 中做了这个更改:

Also in the index.html I did this change:

<!--<div ui-view="theTool"></div>-->
<div ui-view=""></div>

所以现在我们确实有 unnamed ui-view 而不是命名的.这就是为什么两个州

so now we do have unnamed ui-view instead of the named. That's why both states

  • 所有成分
  • 查看

现在可以正确渲染哪个目标未命名视图''.在此示例

which target unnamed view '' are now properly rendered. Se more here in this example

更多关于视图插入逻辑:

More about the the view insertion logic:

小引用:

在幕后,每个视图都被分配了一个遵循 viewname@statename 方案的绝对名称,其中 viewname 是视图指令中使用的名称和状态名称是状态的绝对名称,例如联系方式.项目.您还可以选择以绝对语法编写视图名称.

Behind the scenes, every view gets assigned an absolute name that follows a scheme of viewname@statename, where viewname is the name used in the view directive and state name is the state's absolute name, e.g. contact.item. You can also choose to write your view names in the absolute syntax.

来自同一来源的精彩示例:

Awesome examples from the same source:

.state('contacts.detail', {
  views: {
    ////////////////////////////////////
    // Relative Targeting             //
    // Targets parent state ui-view's //
    ////////////////////////////////////

    // Relatively targets the 'detail' view in this state's parent state, 'contacts'.
    // <div ui-view='detail'/> within contacts.html
    "detail" : { },            

    // Relatively targets the unnamed view in this state's parent state, 'contacts'.
    // <div ui-view/> within contacts.html
    "" : { }, 

    ///////////////////////////////////////////////////////
    // Absolute Targeting using '@'                      //
    // Targets any view within this state or an ancestor //
    ///////////////////////////////////////////////////////

    // Absolutely targets the 'info' view in this state, 'contacts.detail'.
    // <div ui-view='info'/> within contacts.detail.html
    "info@contacts.detail" : { }

    // Absolutely targets the 'detail' view in the 'contacts' state.
    // <div ui-view='detail'/> within contacts.html
    "detail@contacts" : { }

    // Absolutely targets the unnamed view in parent 'contacts' state.
    // <div ui-view/> within contacts.html
    "@contacts" : { }

这篇关于angular-ui/ui-router,如何使用 $stateProvider 注入部分视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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