我在模板中有一个条件如下:
<ng-container>
<p *ngFor="let seat of InfoDetails?.seatInfo">
<template *ngIf="seat.section">
Section {{seat?.section}} ,
</template>
<template *ngIf="seat.row">
Row {{seat?.row}},
</template>
<template *ngIf="seat.seatNo">
Seat number {{seat?.seatNo}}
</template>
</p>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
我有包含row和的数据集seatNo,但它似乎没有在模板中打印.这是什么问题?
我正在尝试构建此模板:
<ul>
<li *ngFor='let link of links'>
<ng-container *ngIf="link.type == 'complex'; then complexLink else simpleLink"></ng-container>
</li>
</ul>
<ng-template #simpleLink>
...
{{ link.some_property }}
</ng-template>
<ng-template #complexLink>
...
{{ link.some_property }}
</ng-template>
Run Code Online (Sandbox Code Playgroud)
问题是在ng-template中未定义链接变量,因此我得到访问未定义的'some_property'的错误.
我很想知道如何将链接变量从ngFor传递到ng-template
很高兴知道这个问题是否有多种解决方案.
如何在angular2 ng-templates中使用以下关键字
$implicit角度2模板的目的是什么?let-<attribute>和之间的关系是什么$implicit?我有一个有角度的4页,包括一个ng-bootstrap模态.我的代码看起来像这样.
foo.html
[...]
<button class="btn btn-sm btn-secondary material-icons"
(click)="open(content)">search</button>
<ng-template #content let-c="close" let-d="dismiss">
content in here
</ng-template>
[...]
Run Code Online (Sandbox Code Playgroud)
foo.ts
[...]
constructor(private modalService: NgbModal) { }
[...]
open(content) {
let options: NgbModalOptions = {
size: 'lg'
};
this.modalService.open(content, options);
}
[...]
Run Code Online (Sandbox Code Playgroud)
现在当我点击按钮时,模态打开.我现在要做的是在ngChanges上打开模型.
ngOnChanges(changes: any) {
open(content)
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:我如何获得"内容"?有没有办法以编程方式获取ng-template?提前致谢!
今天我不得不修复由此代码引起的性能问题:注意在模板内部调用的updateStats
<script type="text/ng-template" id="entityGrouper">
<section>
<div>
<ul ng-click="hideEntityBox = !hideEntityBox">
<li>
{{ entityNode.name }}
</li>
<li ng-repeat="breadcrumbItem in entityNode.breadcrumb">
{{ breadcrumbItem }}
</li>
</ul>
{{ updateStats(entityNode) }}
<span ng-include="'/mypath/views/resume.html'"></span>
</div>
<div>
<div ng-repeat="entityNode in entityNode.group" ng-include="'entityGrouper'"></div>
<div ng-repeat="entity in entityNode.group" ng-include="'entityBox'"></div>
</section>
</script>
Run Code Online (Sandbox Code Playgroud)
模板使用:
<div ng-repeat="entityNode in entityNode.group" ng-include="'entityGrouper'"></div>
Run Code Online (Sandbox Code Playgroud)
在调试这段代码后,我发现这个函数被调用的时间比数组大小多很多(我的数组有4个对象,函数被调用超过100次),甚至鼠标悬停都调用了这个函数.我通过在模板中放置一个ng-init来修复它,现在它正常工作,但我没有弄清楚为什么这个函数被调用了很多次.有没有关于双向数据绑定的东西?
如果使用带有then/else语法的模板,如何将ngFor循环中的当前变量传递给ngIf?
看起来它们在内联时使用时很好,但是无法从模板访问,例如:
<ul *ngFor="let number of numbers">
<ng-container *ngIf="number % 2 == 0; then even_tpl; else odd_tpl"><>/ng-container>
</ul>
<ng-template #odd_tpl>
<li>Odd: {{number}}</li>
</ng-template>
<ng-template #even_tpl>
<li>Even: {{number}}</li>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
模板似乎根本没有访问权限number,但如果内联使用它可以工作.
以下链接中的工作版和非工作版的完整示例:plunkr
angular2如何使用来自不同文件的ng-template?当我将ng-template放在我使用它的同一个HTML中时,但是当我将ng-template移动到一个单独的文件中时,它将无法工作.有没有办法将ng-template移动到自己的文件中并在不同的html文件中使用它?
INFO-message.html
<ng-template #messageTemplate>
Hi
</ng-template>
<ng-container *ngTemplateOutlet="messageTemplate;"></ng-container>
Run Code Online (Sandbox Code Playgroud)
以上工作正常,因为ng-template和用法在同一个文件中
消息template.html
<ng-template #messageTemplate>
Hi
</ng-template>
Run Code Online (Sandbox Code Playgroud)
INFO-message.html
<ng-container *ngTemplateOutlet="messageTemplate;"></ng-container>
Run Code Online (Sandbox Code Playgroud)
这不起作用.有没有办法使用"messageTemplate",它位于另一个html中的单独文件中(例如:info-message.html)
提前致谢.
在我的Angular 6应用中,我需要将Component作为其ng-template传递给另一个Component 。
原因是我有一个组件A,需要多次复制,但是每次它必须包含具有相同Inputs的不同组件(我们称它们为Component B和Component C)。
组件A模板:
<div class="row-detail-panel">
<h4 class="row-detail-panel-title">{{ newEntity ? 'Add new' : 'Edit this'}} {{ entityName }}</h4>
<!--THIS IS THE COMPONENT I WANT TO INJECT-->
<app-component-b
[inline]="true"
[form]="form"
></app-component-b>
<!--END-->
<!--some more html code here-->
</div>
Run Code Online (Sandbox Code Playgroud)
我使用以下方法创建一个Component A实例:
<app-component-a
[entity]="row"
[entityName]="entityName"
></app-component-a>
Run Code Online (Sandbox Code Playgroud)
所以我考虑使用ng-template,因此按如下所示更改Component A模板:
<div class="row-detail-panel">
<h4 class="row-detail-panel-title">{{ newEntity ? 'Add new' : 'Edit this'}} {{ entityName }}</h4>
<ng-template></ng-template> …Run Code Online (Sandbox Code Playgroud) 我想知道最好或推荐的方法是将模板传递给子组件。据我所知,基本上有 3 种方法可以做到这一点:
父级.html
<ng-template #mytemplate>
<!-- some stuff -->
</ng-template>
<child [stuff]="myTemplate"></child>
Run Code Online (Sandbox Code Playgroud)
孩子.ts
@Input() public stuff: TemplateRef<any>;
Run Code Online (Sandbox Code Playgroud)
孩子.html
<ng-container [ngTemplateOutlet]="stuff"></ng-container>
Run Code Online (Sandbox Code Playgroud)
父级.html
<child>
<ng-template #mytemplate>
<!-- some stuff -->
</ng-template>
</child>
Run Code Online (Sandbox Code Playgroud)
孩子.ts
@ContentChild(TemplateRef) layoutTemplate: TemplateRef<any>;
Run Code Online (Sandbox Code Playgroud)
孩子.html
<ng-container *ngTemplateOutlet="layoutTemplate">
</ng-container>
Run Code Online (Sandbox Code Playgroud)
父级.html
<child>
<!-- some stuff -> not ng-template around it! -->
</child>
Run Code Online (Sandbox Code Playgroud)
孩子.ts
// nothing required
Run Code Online (Sandbox Code Playgroud)
孩子.html
<ng-content></ng-content>
Run Code Online (Sandbox Code Playgroud)
我无法在文档中找到有关“标准”方式的任何内容。仅仅是味道吗?其中之一是否有好处使其成为默认方式?
当然,总有一些用例其中一种更适合。但我对上面的例子感兴趣 - 所有这些都可以使用。
是否已经在某处给出了一些文档或建议?看来您在搜索“如何将模板传递给子项”时找到的示例更有可能使用选项 2。但是为什么呢?我猜这是因为他们倾向于展示案例示例,其中模板不仅用于将其传递给子项,而且还用于父项本身。
谢谢!
我正在使用 Angular 7 并尝试获取我已放置在ng-template标签中的参考变量。但它总是返回undefined
测试组件.html
<ng-template #abc>
<div #xyz>
</div>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
测试组件.ts
@ViewChild('abc') abc: ElementRef; //---> works fine
@ViewChild('xyz') xyz: ElementRef; //---> undefined
Run Code Online (Sandbox Code Playgroud)
测试组件.ts
ngAfterViewInit(){
console.log(this.xyz); //---> undefined
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试在 angular 的所有生命周期钩子中打印它,但它总是返回未定义。但是当我尝试将它放在ng-template 的外侧时,它工作得很好。
有什么办法吗?
ng-template ×10
angular ×9
ng-bootstrap ×2
typescript ×2
angularjs ×1
javascript ×1
modal-dialog ×1
ngfor ×1
parent-child ×1