简体   繁体   English

AngularJS ui-router查看自定义数据

[英]AngularJS ui-router views with custom data

I have an AngularJS app and use the ui-router for routing. 我有一个AngularJS应用程序并使用ui-router进行路由。 One of my pages has two similar sections with the same template and logic (so I hope they can use the same controller). 我的一个页面有两个相似的部分,具有相同的模板和逻辑(所以我希望他们可以使用相同的控制器)。 The only difference between them is for example type property. 它们之间的唯一区别是例如type属性。 Here is simplified fiddle of the page: http://jsfiddle.net/e_gluhotorenko/XeX59/7/ . 这是页面的简化小提琴: http//jsfiddle.net/e_gluhotorenko/XeX59/7/

So Is it possible to provide custom different data to the views's scopes ? 那么是否可以为视图的范围提供自定义的不同数据? Something like custom data in states but for views: 像州内的自定义数据,但视图:

views: {
    'section1' : {
        templateUrl: 'section.html',
        controller : 'ctrl',
        data : { type: 'type1'}
    },
    'section2' : {
        templateUrl: 'section.html',
        controller : 'ctrl',
        data : { type: 'type2'}
    }
}

Or with ui-view directive like in ng-inclide 's onload : 或者像ng-inclideonload一样使用ui-view指令:

<div ui-view="section1" onload="type = 'type1'"></div>
<div ui-view="section2" onload="type = 'type2'"></div>

Just found out that ui-view actually has onload attribute, it is absent in documentation but is in API ref . 刚刚发现ui-view实际上有onload属性,它在文档中不存在但在API ref中 So my example from the question works! 所以这个问题的例子有效! fiddle 小提琴

Did you try ng-init as in <div ui-view="section1" ng-init="type = 'type1'"></div> ? 您是否在<div ui-view="section1" ng-init="type = 'type1'"></div>尝试ng-init That should work, I would link the angular docs for ng-init , but the site seems to be down right now 这应该工作,我会链接ng-init的角度文档,但该网站似乎现在正在下降

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

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