我只想将每个传递的组件元素包装到" div"和#34;一些" css课。 例如: 我转到了组件组件 - 包装儿童'三要素:
<component-that-wrap-children>
<input type="text" [(ngModel)]="name"/>
<div>text</div>
<some-component>another text</some-component>
</component-that-wrap-children>
然后我希望它变成:
<component-that-wrap-children>
<div class="some"><input type="text" [(ngModel)]="name"/></div>
<div class="some"><div>text</div></div>
<div class="some"><div>another text</div></div>
</component-that-wrap-children>
有办法吗?
UPD 使用React我可以这样做:
import React, {Component, PropTypes} from 'react';
class ComponentThatWrapChildren extends Component {
render() {
let rows = this.props.children.map((child, i) => <div key={i} className="some">{child}</div>);
return (<div>{rows}</div>);
}
}
ComponentThatWrapChildren.propTypes = {
children: PropTypes.node
};
export default ComponentThatWrapChildren;
&#13;