Angular 使用时<;应用程序儿童>;用于绑定@Input it';s模板在其他所有组件中都可见
子组件Angular 使用时<;应用程序儿童>;用于绑定@Input it';s模板在其他所有组件中都可见,angular,input,angular-components,Angular,Input,Angular Components,子组件 selector: 'app-child', template: ` <div class="container"> <div class="table"> <thead class="thead-light"> <tr> <th>Name</th> <th>
selector: 'app-child',
template: `
<div class="container">
<div class="table">
<thead class="thead-light">
<tr>
<th>Name</th> <th>Paswword</th>
</tr>
</thead>
<tbody *ngFor="let info of loginInfo">
<tr>
<td>{{info.name}}</td>
<td>{{info.password}}</td>
</tr>
</tbody>
</div>
</div>`,
styleUrls: ['./child.component.css'],
// encapsulation: ViewEncapsulation.None // ** nome, shadowdom, native
})
export class ChildComponent implements OnInit {
@Input() loginInfo: { name: string, password: string };
selector: 'app-root',
template: ` <app-child [loginInfo]="loginDetails"></app-child> `,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'lifecycle';
loginDetails = [
{ name: 'qwe', password: '123' },
{ name: 'rty', password: 'pom@123' },
];
}
选择器:'app child',
模板:`
帕斯沃德
{{info.name}
{{info.password}}
`,
样式URL:['./child.component.css'],
//封装:ViewEncapsulation.None//**nome、shadowdom、native
})
导出类ChildComponent实现OnInit{
@Input()登录信息:{name:string,password:string};
应用程序组件
selector: 'app-child',
template: `
<div class="container">
<div class="table">
<thead class="thead-light">
<tr>
<th>Name</th> <th>Paswword</th>
</tr>
</thead>
<tbody *ngFor="let info of loginInfo">
<tr>
<td>{{info.name}}</td>
<td>{{info.password}}</td>
</tr>
</tbody>
</div>
</div>`,
styleUrls: ['./child.component.css'],
// encapsulation: ViewEncapsulation.None // ** nome, shadowdom, native
})
export class ChildComponent implements OnInit {
@Input() loginInfo: { name: string, password: string };
selector: 'app-root',
template: ` <app-child [loginInfo]="loginDetails"></app-child> `,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'lifecycle';
loginDetails = [
{ name: 'qwe', password: '123' },
{ name: 'rty', password: 'pom@123' },
];
}
选择器:“应用程序根目录”,
模板:``,
样式URL:['./app.component.css']
})
导出类AppComponent{
标题=‘生命周期’;
登录详细信息=[
{名称:'qwe',密码:'123'},
{名称:'rty',密码:'pom@123' },
];
}
使用子选择器标记绑定@Input()时,子组件模板将显示在其他每个组件中。有没有办法只在子组件中显示它?应用程序组件在所有组件之间共享,因此如果在其模板视图中插入组件选择器,所有其他组件都会看到它; 如果希望其他组件看不到子模板,则必须 不要在应用程序组件模板中插入它
您必须创建另一个组件,您必须在其中放置应用程序子选择器,然后在加载应用程序组件时调用它 应用程序内组件路由-模块
const routes: Routes = [
{
path: 'newComponent',
loadChildren: () => import('app/newComponent/newComponent.module')
.then(m => m.NewComponentModule),
}
]
所以当应用程序组件被自动加载时,新组件被加载您可以使用另一个组件绑定@Input变量。比如: 1) AppComponent=并行组件 2) 创建组件(BillComponent),然后定义模板:
,在它里面
3) 为BillComponent创建布线:
{path: 'generate-bill', component: BillComponent},
内部应用程序路由.module.ts
4) 现在,billComponent.ts文件如下所示:
selector: 'app-bill',
template: ` <app-child [loginInfo]="loginDetails"></app-child> `,
styleUrls: ['./bill.component.css']
})
export class BillComponent {
title = 'lifecycle';
loginDetails = [
{ name: 'qwe', password: '123' },
{ name: 'rty', password: 'pom@123' },
];
}
选择器:“应用程序账单”,
模板:``,
样式URL:['./bill.component.css']
})
导出类BillComponent{
标题=‘生命周期’;
登录详细信息=[
{名称:'qwe',密码:'123'},
{名称:'rty',密码:'pom@123' },
];
}
5) 子组件将与您在代码中提到的相同
注意:请检查组件流。如parenet、兄弟或子组件。谢谢。但是如何将@Input变量从child绑定到app组件呢?您必须创建另一个组件,在其中放置app child选择器,然后在加载app组件时调用它。