gpt4 book ai didi

angularjs - 如何将特定绑定(bind)传递给 Angular 1.5 中组件路由器引入的 ng-outlet 组件?

转载 作者:行者123 更新时间:2023-12-03 08:16:48 25 4
gpt4 key购买 nike

我希望能够将参数传递给使用 ng-outlet 引入的子组件。但我不知道该怎么做。

这是我的组件绑定(bind)的示例:

app.component('profile', {
bindings: {
section: '=',
currentUser: '<'
},
...

通常我会这样调用它:
<profile section="$ctrl.bio" current-user="$ctrl.selectedUser"></profile>

但相反,我有这个:
<ng-outlet></ng-outlet>

和一个传递配置文件的路由器。
 $routeConfig: [
{ path: '/Profile/:id', name: 'Profile', component: 'profile' }]

那么我如何将其他基本绑定(bind)(可能是无法编码到 URL 中的绑定(bind))传递给这个组件?

谢谢,非常感谢帮助

编辑:我被要求提供一个更具体的例子来说明我想做的事情。

我认为概念问题相当清楚,但这是一个特殊情况,其中传递路由参数显然是不够的。假设在我的 App 组件级别我有一个事件回调函数 onDeleteItem(id)

我如何复制
bindings: {
onDeleteItem: "&"
}

...

<some-component on-delete-item="$ctrl.onDeleteItem(id)"></some-component>

带 ng socket ?

最佳答案

我看了docs并看到了路由器中使用的组件之一

bindings: { $router: '<' }



所以我在想如果该绑定(bind)可以被填充,那么为什么我们不能为其他绑定(bind)传递数据,但是我查看了组件路由器的源,实际上我没有看到传递数据的方法方式。这是因为这里看起来像是 specially handling passing in the $router ,但我认为此后不会修改模板:
activate(instruction) {
...
this.controller.$$template = '<' + dashCase(componentName) + ' $router="::$$router"></' + dashCase(componentName) + '>';
...
};

不过我会说,您应该能够使用
$routeConfig: [
{ path: '/Profile/:id', name: 'Profile', component: 'profile', data: { item1: 'item1'} }]

然后注入(inject) RouteData在组件的构造函数中,但我认为这仅在 angular 2 中,因为我没有在 angular 1 路由器中看到它的实现。

所以根据我所做的研究,我认为你不能,或者很难。另外,相关: How to inject data into Angular2 component created from a routerhttps://github.com/angular/angular/issues/4452

关于angularjs - 如何将特定绑定(bind)传递给 Angular 1.5 中组件路由器引入的 ng-outlet 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050333/

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