gpt4 book ai didi

javascript - Angular JS 路由 : attach more than one controller

转载 作者:行者123 更新时间:2023-11-30 10:08:44 24 4
gpt4 key购买 nike

是否可以将多个 Controller 附加到一个路由,例如一组 Controller ?

'/store'中,我需要附上"StoreController"和"ReviewController",不用重复.when()这么多次:

我的代码:

angular  
.module('gemStore', [
'example-directives'
])
.config(function ($routeProvider) {

$routeProvider

.when('/store', {
templateUrl: 'store.html',
controller: 'StoreController'
})

.when('/store', {
templateUrl: 'store.html',
controller: 'ReviewController'
})

.otherwise({
redirectTo: '/'
});

});

最佳答案

应该发生的是你应该只有一个页面的“根” Controller ,并且页面的任何“部分”都应该是根 Controller 的子 Controller 。

$routeProvider

.when('/store', {
templateUrl: 'store.html',
controller: 'StoreParentController'
})

然后 child 可以像这样在 HTML 中声明:

<div ng-controller="StoreParentController"> <!-- you dont need this particular controller here because you have it in your routeprovider, this is just an example of the structure-->
<div ng-controller="StoreController"> stuff about store </div>
<div ng-controller="ReviewsController"> reviews </div>
</div>

关于javascript - Angular JS 路由 : attach more than one controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27693164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com