问题描述
我希望能够将参数传递给ng-outlet带来的子组件.但是我不确定该怎么做.
I'd like to be able to pass parameters to child components brought in with ng-outlet. But I'm not sure how to do it.
这是我的组件绑定的示例:
Here's an example of my component bindings:
app.component('profile', {
bindings: {
section: '=',
currentUser: '<'
},
...
通常我会这样调用:
<profile section="$ctrl.bio" current-user="$ctrl.selectedUser"></profile>
但是我有这个:
<ng-outlet></ng-outlet>
还有一个将配置文件传入的路由器.
And a router that passes the profile in.
$routeConfig: [
{ path: '/Profile/:id', name: 'Profile', component: 'profile' }]
那么,如何将其他必需的绑定(可能是无法编码为URL的绑定)传递给该组件呢?
So how the blazes do I pass other essential bindings, perhaps bindings that can't be encoded into the URL, to this component??
谢谢,非常感谢您的帮助
Thanks, help is very much appreciated
我被要求提供一个我想做的更具体的例子.
我认为概念上的问题很明确,但是在一个特殊情况下,传递路径参数显然不足.在我的应用程序组件级别说,我有一个事件回调函数onDeleteItem(id)
I thought the conceptual issue was fairly clear, but here's a particular case where passing route parameters is clearly insufficient. Say at my App component level I have an event callback function, onDeleteItem(id)
我如何复制
bindings: {
onDeleteItem: "&"
}
...
<some-component on-delete-item="$ctrl.onDeleteItem(id)"></some-component>
带有ng-outlet?
with an ng-outlet?
推荐答案
我查看了文档,并在路由器中使用的组件之一中看到了这一点
I looked on the docs and saw this for one of the components used in the router
所以我一直在想如果绑定可以被填充,那为什么我们不能传递其他绑定的数据,但是我仔细查看了组件路由器的来源,实际上我没有找到一种方法.这样传递数据.这是因为在这里看起来它是特殊处理的传入$ router ,但是我不认为此后会修改模板:
So I was thinking well if that binding can get filled in, then why can't we pass data for the other bindings, but I looked through the source of the component router, and I actually don't see a way to pass data that way. This is because here it looks like it's specially handling passing in the $router, but I don't think the template is modified after that:
activate(instruction) {
...
this.controller.$$template = '<' + dashCase(componentName) + ' $router="::$$router"></' + dashCase(componentName) + '>';
...
};
不过,我会说,您应该能够使用
I will say though, you are supposed to be able to pass data through to the component using
$routeConfig: [
{ path: '/Profile/:id', name: 'Profile', component: 'profile', data: { item1: 'item1'} }]
,然后在构造函数中注入 RouteData 组件,但我认为这仅在角度2中使用,因为我没有在角度1路由器中看到它的实现.
and then injecting RouteData in the constructor of the component, but I think this is only in angular 2, because I didn't see implementation for it the in the angular 1 router.
因此,根据我所做的研究,我认为您无法做到,否则就很难.另外,与以下内容相关:如何将数据注入由路由器创建的Angular2组件和 https://github.com/angular/angular/issues/4452
So based on the research I've done, I don't think you can, or it's difficult. Also, related: How to inject data into Angular2 component created from a router and https://github.com/angular/angular/issues/4452
这篇关于如何将特定的绑定传递给Angular 1.5中的组件路由器带来的ng-outlet组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!