简体   繁体   English

传递和显示角度组件5

[英]Passing and displaying components angular 5

I have a scenario where I need to pass a list of different "components" to another angular component. 我有一种情况,我需要将不同的“组件”列表传递给另一个角度组件。 I'm passing the list of components as an array using the @Input. 我使用@Input将组件列表作为数组传递。 How can I render those components in the HTML? 如何在HTML中呈现这些组件?

It would be helpful to see some code to have an idea about what you're trying to accomplish. 看到一些代码来了解您要完成的工作会很有帮助。

From the documentation, @Input() 从文档中, @ Input()

  • An Input property is a settable property annotated with an @Input decorator. Input属性是用@Input装饰器注释的可设置属性。 Values flow into the property when it is data bound with a property binding 当数据与属性绑定绑定时,值会流入属性

An @Input() annotation is used for passing data. @Input()批注用于传递数据。 It's not used for passing components to another component. 它不用于将组件传递到另一个组件。

To display another component inside another component, reference the nested component's selector inside the parent component. 要在另一个组件内显示另一个组件,请在父组件内引用嵌套组件的选择器。 The selector is part of component metadata 选择器是组件元数据的一部分

  • selector : A CSS selector that tells Angular to create and insert an instance of this component wherever it finds the corresponding tag in template HTML. selector :CSS选择器,它告诉Angular在模板HTML中找到相应标签的任何地方创建并插入此组件的实例。 For example, if an app's HTML contains , then Angular inserts an instance of the HeroListComponent view between those tags. 例如,如果应用程序的HTML包含,则Angular会在这些标签之间插入HeroListComponent视图的实例。

app-hero-list.component.ts app-hero-list.component.ts

Create a component and give a selector in the metadata. 创建一个组件并在元数据中提供一个选择器。

@Component({
  selector:    'app-hero-list',
  templateUrl: './hero-list.component.html',
  providers:  [ HeroService ]
})

app.component.html app.component.html

Then you can reference that selector as a pair of HTML tags in another component's template. 然后,您可以将该选择器引用为另一个组件模板中的一对HTML标记。

<app-hero-list></app-hero-list>

Have a look at the component metadata link referenced above and the Angular Tutorial for some more information about displaying components in other components. 查看上面引用的组件元数据链接和Angular教程,以获取有关在其他组件中显示组件的更多信息。

If you need a more detailed answer, please update your question with a code example. 如果您需要更详细的答案,请使用代码示例更新您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM