angular中动态传入模板-ngTemplateOutlet

本文介绍Angular中如何使用ngTemplateOutlet实现在组件中动态传递参数,通过具体示例展示了ngTemplateOutlet的基本用法及如何在模板中绑定上下文变量,使组件更加灵活。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在写公共组件的时候可以通过ng-content实现类似vue的slot的效果,但是使用ng-content不能动态的传参,所以在写组件的时候会受到很大的限制,而ngTemplateOutlet可以动态传参

ngTemplateOutlet

插入ng-template创建的内嵌视图

简单例子

<div *ngTemplateOutlet="tpl1"></div>
<ng-template #tpl1>
  <span>I am span in template {{title}}</span>
</ng-template>

按照官方文档,其语法知识如下: *ngTemplateOutlet = "templateRefExp; content: contentExp "

    templateRefExp:  ng-template 元素的#ID

    contextExp:

      1、可空参数; 

      2、可以在模版中使用 let-key 语句进行绑定; 这个key,是我们在HTML模版绑定显示的key,即 {{key}} 。

      3、使用 $implicit 这个key会把对应的值设置为默认值;

         因为我们会为content指定一个对象,每个对象可能有一个或多个值;

         如果这个对象,是一个值,则不需显示指定名称,如 student :  { name: 'jack' } ,可以用 student: { $implicit: 'jack' };

        在 ng-template 中也不必使用 let-showName = "name", 只需要 let-showName 即可。 

          如下例子:

   HTML文件:
      <ng-container *ngTemplateOutlet="tplStu; context: student"></ng-container>
      <ng-template #tplStu let-name let-ageHTML="age">hello {{name}},your age is {{ageHTML}}</ng-template>

    .ts文件

         student = { $implicit: 'jack' , age: '19'};

 

Angular中,实现这样的功能,你可以创建一个自定义的子组件,并利用@Input()属性接收外部传来的数据,以及EventEmitter或Output()接口来监听并发送事件。这里是一个简单的步骤说明: 1. **创建子组件**: 首先,在`src/app`目录下创建一个新的文件夹(假设名为`custom-input`),然后在其中创建一个名为`custom-input.component.ts`的文件,编写如下: ```typescript import { Component, Input, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'app-custom-input', template: ` <div> <label>{{ label }}</label> <input type="text" [(ngModel)]="value"> <button (click)="onValueChanged()">提交</button> </div> `, styleUrls: ['./custom-input.component.css'] }) export class CustomInputComponent { @Input() label: string; // 接收外部传来的标签文本 @Output() valueChanged = new EventEmitter<string>(); // 输出事件 onValueChanged() { this.valueChanged.emit(this.value); // 当输入改变时触发事件,并传递当前值到父组件 } } ``` 2. **添加模板引用**: 在`custom-input.component.html`中,引入`<ng-template>`或直接使用`<ng-container>`来绑定事件处理函数,例如: ```html <!-- 或者使用 ng-template --> <ng-template #onClick let-value="value"> <!-- 父组件模板--> <button (click)="handleValueChange(value)">提交</button> </ng-template> <!-- 或者直接在 HTML 模板上绑定 --> <button [ngTemplateOutlet]="onClick" [ngTemplateOutletContext]="{ value: $event }"></button> ``` 3. **在父组件中使用**: 在需要使用子组件的地方,导入并声明`CustomInputComponent`,并将其包含在模板中,同时绑定事件处理函数: ```typescript import { Component } from '@angular/core'; import { CustomInputComponent } from './custom-input/custom-input.component'; @Component({ selector: 'app-parent-component', template: ` <app-custom-input [label]="'请输入信息:'" (valueChanged)="handleValue($event)"></app-custom-input> `, styleUrls: ['./parent-component.component.css'] }) export class ParentComponent { handleValue(value: string) { console.log('从子组件接收到的值:', value); // 这里可以进一步操作接收到的值 } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值