如何将属性从包装器组件透明地转换为嵌套组件?
考虑到有
const FIRST_PARTY_OWN_INPUTS = [...];
const FIRST_PARTY_PASSTHROUGH_INPUTS = ['all', 'attrs', 'are', 'passed'];
@Component({
selector: 'first-party',
inputs: [...FIRST_PARTY_OWN_INPUTS, ...FIRST_PARTY_PASSTHROUGH_INPUTS],
template: `
<div>
<third-party [all]="all" [attrs]="attrs" [are]="are" [passed]="passed"></third-party>
<first-party-extra></first-party-extra>
</div>
`,
directives: [ThirdParty]
})
export class FirstParty { ... }是否可以批量翻译输入,这样它们就不会在模板中被枚举?
上面的代码是为了重新创建Angular 1.x指令的配方:
app.directive('firstParty', function (thirdPartyDirective) {
const OWN_ATTRS = [...];
const PASSTHROUGH_ATTRS = Object.keys(thirdPartyDirective[0].scope);
return {
scope: ...,
template: `
<div>
<third-party></third-party>
<first-party-extra></first-party-extra>
</div>
`,
compile: function (element, attrs) {
const nestedElement = element.find('third-party');
for (let [normalizedAttr, attr] of Object.entries(attrs.$attr)) {
if (PASSTHROUGH_ATTRS.includes(normalizedAttr)) {
nestedElement.attr(attr, normalizedAttr);
}
}
},
...
};
});发布于 2016-07-04 13:52:51
我不确定是否正确,但下面是我的实现( )
const FIRST_PARTY_OWN_INPUTS = ['not', 'passthrough'];
const FIRST_PARTY_PASSTHROUGH_INPUTS = ['all', 'attrs', 'are', 'passed'];
const generateAttributes(arr) {
return arr.map(att => '[' + att + '] = "' + att + '"').join(' ');
}
//-------------------------------------------------------//////////////////
import {Component} from '@angular/core'
@Component({
selector: 'third-party',
inputs: [...FIRST_PARTY_PASSTHROUGH_INPUTS],
template: `
<div>
{{all}} , {{attrs}} , {{are}} , {{passed}}
</div>
`
})
export class ThirdParty {
}
@Component({
selector: 'first-party',
inputs: [...FIRST_PARTY_OWN_INPUTS, ...FIRST_PARTY_PASSTHROUGH_INPUTS],
template: `
<div>
<div>
{{not}} , {{passthrough}}
</div>
<third-party ${generateAttributes(FIRST_PARTY_PASSTHROUGH_INPUTS)}></third-party>
<first-party-extra></first-party-extra>
</div>
`,
directives: [ThirdParty]
})
export class FirstParty {
}
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<h2>Hello {{name}}</h2>
<first-party [not]="'not'" [passthrough]="'passthrough'"
[all]="'all'" [attrs]="'attrs'" [are]="'are'" [passed]="'passed'">
</first-party>
</div>
`,
directives: [FirstParty]
})
export class App {
constructor() {
this.name = 'Angular2 (Release Candidate!)'
}
}希望能对:)有所帮助
发布于 2016-07-09 20:58:10
我认为这可以归结为一个更基本的问题,根本没有Angular2。当你有一个带有许多参数的函数时,每次你想要使用它时都必须指定所有这些参数,这是令人讨厌的,而且容易出错。当有一个中间函数根本不关心这些参数时,问题会变得更糟--你发现自己向中间函数添加了参数,这样它就可以将参数传递给内部函数。太棒了!
有几种模式可以处理这个问题。我最喜欢的是完全实例化内部函数,并传递已经加载的实例,其中嵌入了以前的传递参数。我认为http://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/是一篇很好的文章,讲述了如何在Angular 2中使用@ViewChild和@ContentChild来做到这一点。另一种策略是将所有传递参数包装在单个对象中,因此至少只有一个参数要传递。当您想要添加更多参数时,这也很有帮助--因为它们已经被包装起来并以不透明的方式传递,所以您的直通代码不需要更改。
发布于 2016-07-04 22:05:33
您可以通过对子组件使用@Input()来实现这一点。
http://plnkr.co/edit/9iyEsnyEPZ4hBmf2E0ri?p=preview
父组件:
import {Component} from '@angular/core';
import {ChildComponent} from './child.component';
@Component({
selector: 'my-parent',
directives: [ChildComponent],
template: `
<div>
<h2>I am the parent.</h2>
My name is {{firstName}} {{lastName}}.
<my-child firstName="{{firstName}}"
lastName="{{lastName}}">
</my-child>
</div>
`
})
export class ParentComponent {
public firstName:string;
public lastName: string;
constructor() {
this.firstName = 'Bob';
this.lastName = 'Smith';
}
}子组件:
import {Component, Input} from '@angular/core';
@Component({
selector: 'my-child',
template: `
<div>
<h3>I am the child.</h3>
My name is {{firstName}} {{lastName}} Jr.
<br/>
The name I got from my parent was: {{firstName}} {{lastName}}
</div>
`
})
export class ChildComponent {
@Input() firstName: string;
@Input() lastName: string;
}应用组件:
//our root app component
import {Component} from '@angular/core';
import {ParentComponent} from './parent.component';
@Component({
selector: 'my-app',
directives: [ParentComponent],
template: `
<div>
<my-parent></my-parent>
</div>
`
})
export class App {
constructor() {
}
}https://stackoverflow.com/questions/38122124
复制相似问题