简体   繁体   English

如何在Angular中将对象属性用作@Input绑定?

[英]How to use an object property as @Input binding in Angular?

I'm new in Angular and Ionic Frameworks, so I'm practicing first. 我是Angular和Ionic框架的新手,所以我先练习。 I have a trouble with a basic @Input testing where basically I'm looping trought an array of Tab Pages and then I want to render each tab with <ion-tab> . 我在基本的@Input测试中遇到了麻烦,基本上我正在循环遍历标签页数组,然后想用<ion-tab>渲染每个标签。 Here is my code: 这是我的代码:

tabs.html tabs.html

<ion-tabs>
  <ion-tab *ngFor="let page of tabPages" [root]="page.root" [tabTitle]="page.title">
  </ion-tab>
</ion-tabs>

tabs.ts tabs.ts

import { Component }    from '@angular/core';

// - - - Pages Components - - - //
import { AboutPage }    from '../about/about';
import { ContactPage }  from '../contact/contact';
import { HomePage }     from '../home/home';
import { SettingsPage } from "../settings/settings";

@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {

  tabPages : Array<any>;

  constructor() {
    this.tabPages = [];

    this.tabPages.push( { root : HomePage, title : "Home" } );
    this.tabPages.push( { root : AboutPage, title : "About" } );
    this.tabPages.push( { root : ContactPage, title : "Contact" } );
    this.tabPages.push( { root : SettingsPage, title : "Settings" } );
  }
}

So my question is there's a way to bind a property from an object and used it as Input for a component? 所以我的问题是,有一种方法可以绑定对象的属性并将其用作组件的输入?

Thanks for reply. 谢谢您的回复。

Yes that is possible, 是的,那是可能的,

<ng-container *ngFor="let page of tabPages" >
  <ion-tab [root]="page.root" [tabTitle]="page.title">
  </ion-tab>
</ng-container>

and your child component should have something like, 并且您的子组件应该具有类似的内容,

@input root: string;
@input tabTitle: string;

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

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