我想在 Angular 2 中制作一个 TableRowsComponent 来呈现多个表行。
我想在这样的模板中使用它:
<table class>
<tbody>
<table-rows *ngFor="#item of list" [someInput]="item"></table-rows>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
(因此“table-rows”将是此处 TableRowsComponent 的选择器)TableRowsComponent 的模板将类似于:
<tr>
<td> foo </td>
<td> bar </td>
</tr>
<tr *ngIf="something">
....
</tr>
<tr *ngFor="..">
...
</tr>
Run Code Online (Sandbox Code Playgroud)
如果我们这样做,那么结果如下所示:
<table class>
<tbody>
<table-rows>
<tr>
...
</tr>
<tr>
...
</tr>
</table-rows>
<table-rows>
<tr>
...
</tr>
<tr>
...
</tr>
</table-rows>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
不幸的是, < table-rows > 元素搞乱了表格的渲染。如何解决这个问题呢?有没有办法让 angular2 不渲染 < table-rows > 元素?
我有一个 NgSwitch 模板。在 NgSwitch 中,我想获取对初始化模板的模板引用。像这样的东西:
<div class="container" [ngSwitch]="model.type">
<first-component #ref *ngSwitchCase="0"></first-component>
<second-component #ref *ngSwitchCase="1"></second-component>
<third-component #ref *ngSwitchCase="2"></third-component>
</div>
Run Code Online (Sandbox Code Playgroud)
当单击组件中的按钮时,我想调用初始化组件(第一/第二/第三)的方法(该方法在所有这三个组件实现的接口上定义)。问题是 ViewChild 未定义。如果我将 #ref 移动到容器 div,如下所示:
<div class="container" #ref [ngSwitch]="model.type">
<first-component *ngSwitchCase="0"></first-component>
<second-component *ngSwitchCase="1"></second-component>
<third-component *ngSwitchCase="2"></third-component>
</div>
Run Code Online (Sandbox Code Playgroud)
ViewChild(模板引用)已初始化,但随后我可以调用组件的方法。
如何同时使用 NgSwitch 指令和模板引用变量?或者另一方面,如何从其父级调用已初始化的组件(在这种情况下,我将 #ref 移动到容器 div)。
相当标准的情况。
有一个父组件<item-list>。在其模板内*ngFor生成了 20 个子组件<item-block>。[ngStyle]使用调用 function 的指令和模板表达式设置子组件样式setStyles()。
问题(或可能不是)是,当在一个特定子元素上发出任何事件时,表达式setStyles()会对每个子组件执行两次。
因此,如果我们单击示例中的一个特定项目,并且我们有 20 个<item-block>组件 -setStyles()将执行 20+20 次。
问题是:
示例&plnkr:
import {Component} from '@angular/core'
@Component({
selector: 'item-list',
template: `
<item-block
[item]="item"
*ngFor="let item of items"
></item-block>
`,
})
export class ItemListComponent {
items: any[] = [];
constructor() {}
ngOnInit() {
// generate dummy empty items
for (let i = …Run Code Online (Sandbox Code Playgroud) angular2-template angular2-changedetection angular2-components angular
我在 Angular2 中开发了一个使用 form 标签的页面,并且在 table 标签中使用 ngFor 渲染了输入控件。
\n\n我创建了模型(DataModal)来表示表的每一行。我创建了模型列表(DataModalList),并通过单击按钮(addNewRow)不断将每个模型添加到其中
\n\n我面临添加新行时删除选定输入值的问题。\n当我删除表单标签时一切正常\n我需要表单标签来执行验证。
\n\n请找到下面的代码:
\n\n模型:
\n\nimport {DropDownModel} from \'../models/dropDownModel\';\xc2\xa0\nexport class DataModal {\n dropdown: DropDownModel[];\n selectedValue: string;\n constructor() {\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa0 \n this.selectedValue = "0";\xc2\xa0\xc2\xa0\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n成分:
\n\naddNewRow() {\xc2\xa0\n let dataModal = new DataModal();\n dataModal.dropdown = response; //values are coming from api\n this.DataModalList.push(dataModal);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n网页:
\n\n<form novalidate #form="ngForm" (ngSubmit)="submitEditForm(form.valid)">\n <table>\n <tr *ngFor="let item of DataModalList;let i= index ">\n <td class="col-md-3">\n <div>\n <label class="control-label" …Run Code Online (Sandbox Code Playgroud) html angular2-forms angular2-directives angular2-template angular
我创建了一个包含表单的自定义组件<address></address>。我有一个父组件,其中包含以下数组:
@ViewChildren(AddressComponent) addressComponents: QueryList<AddressComponent>;
Run Code Online (Sandbox Code Playgroud)
因此,父级可以包含这些元素的集合,并且用户可以根据他们将输入的地址数量添加和删除它们。
家长还有一个按钮,可以在用户输入所有所需的地址后继续。但是,该<address>组件必须正确填写<address>,因此我在该组件上有一个公共 getter :
get valid(): boolen {
return this._form.valid;
}
Run Code Online (Sandbox Code Playgroud)
回到父级上的按钮。<address>如果任何组件无效,则需要禁用它。所以我写了以下内容:
get allValid() {
return this.addressComponents && this.addressComponents.toArray().every(component => component.valid);
}
Run Code Online (Sandbox Code Playgroud)
在父模板中:
<button [disabled]="!allValid" (click)="nextPage()">Proceed</button>
Run Code Online (Sandbox Code Playgroud)
但 Angular 不喜欢这样,因为addressComponents直到生命周期事件才在父级中定义ngAfterViewInit。由于它立即运行,ngOnViewInit()我得到了导致错误的表达式检查的两个不同值。(至少我认为是这样)。
如何在模板中使用依赖于 的属性ngAfterViewInit?或者告诉我的父母其所有孩子都是有效的最好方法是什么?
错误消息:
检查后表情发生了变化。以前的值:“假”。当前值:“真”
更新:
所以我console.log编辑了 的返回值allValid并第一次注意到它是undefined。这是预料之中的,this.addressComponents直到。这是下一个日志,这令人惊讶,因为我的页面上还没有任何组件。我正在父组件中使用模拟数据(尽管全部有效)来创建组件。我确实了解到(在空数组上返回 true)。所以对 的第三次调用正在返回。我再次感到有点惊讶,因为我的所有数据都是有效的。在第四条日志中,它返回了,这正是我所期望的。所以我假设返回的最终值是 Angular 不喜欢的。undefined …
我对 Angular 还很陌生,我想知道是否有任何方法可以将 ngFor 循环增加 2 而不是 1。
我正在尝试在一个分页中实现两个分页,为此需要将循环增加 2。
我正在获取包含带有 in 的对象的对象。假设用户及其地址列表。
(第一个NgFor是将用户分页2。如果我有15个用户。2个用户将显示在一个页面中)
li *ngFor="let user of Users | paginate: { itemsPerPage: 1, currentPage: p };let i = index"
{{user[i].firstName}} <--Displaying first userName-->
Run Code Online (Sandbox Code Playgroud)
(第二个 NgFor 是显示第一个用户的地址列表。假设他有 20 个地址。我将把它切成每页 3 或 4 个,并对其余部分进行分页)
*ngFor="let address of user[i].address| objectValues |slice:addresspageNumber*2-2:addressPageNumber*5 ; let j = index "
<--First child Pagination code-->
{{user[i+1].firstName}} <--Displaying second userName-->
Run Code Online (Sandbox Code Playgroud)
(第三个 NgFor 是显示第二个用户的地址列表。假设他有 20 个地址。我将把它切成每页 3 或 4 个,并对其余部分进行分页)
*ngFor="let address of user[i+1].address| objectValues |slice:addresspageNumber*2-2:addressPageNumber*5 ; let …Run Code Online (Sandbox Code Playgroud) 我创建了一个带有上下文的结构指令。
@Directive({selector: '[myDir]'})
export class MyDir implements OnInit {
constructor(private templateRef: TemplateRef<any>,
private viewContainerRef: ViewContainerRef,
) { }
public context = {$implicit: false}
ngOnInit() {
this.viewContainerRef.createEmbeddedView(this.templateRef, this.context)
}
}
Run Code Online (Sandbox Code Playgroud)
我就是这样使用的。
<pre *myDir="let context">Context: {{ context | json}}</pre>
Run Code Online (Sandbox Code Playgroud)
模板已正确安装到视图中并按Context: false预期显示。
但是,如果我尝试更改上下文,它不会反映在视图中。ngTemplateOutlet从 Angular repo 的规范来看,它看起来应该在上下文发生变化时更新。我的代码是否有错误,或者我误解了规范?
我也尝试过注入 CR 并手动触发更改检测,但没有成功。在我的真实案例中,更改应该在监听鼠标事件的可观察订阅上触发,但这里有一个具有简单间隔的演示。
Observable
.interval(500)
.map((_, i) => i % 2 == 0)
.takeUntil(this.destroy$)
.subscribe(context => {
this.context = {$implicit: context}
console.log('Context is now', this.context.$implicit)
})
Run Code Online (Sandbox Code Playgroud)
尽管context$ 确实发生了 …
我知道了类似的问题,这是所有 在 该 网站,但我的问题是从那些稍有不同。
我想编写一个为行提供服务的组件,但每个实例可能会生成一行到多行,具体取决于提供的数据:
<tbody>
<!-- intended usage -->
<data-rows *ngFor="let row of dataRows" [row]="row">
</data-rows>
</tbody>
Run Code Online (Sandbox Code Playgroud)
需要创建:
<tbody>
<!-- from first <data-rows> instance -->
<tr><td> instance 1, row 1 </td></tr>
<!-- from second <data-rows> instance -->
<tr><td> instance 2, row 1 </td></tr>
<tr><td> instance 2, row 2 </td></tr>
<tr><td> instance 2, row 3 </td></tr>
<!-- from third <data-rows> instance -->
<tr><td> instance 3, row 1 </td></tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)
大多数解决方案建议使用属性选择器并在真正的 <tr> 上使用 *ngFor。这在我的情况下不起作用,因为实例和行之间没有一对一的关系。另外,父组件不知道应该渲染多少个<tr>;这是由 <data-rows> 决定的。
当然,一个简单的 <data-rows> 实现会失败,因为它会向 …
假设我们prop在组件类中有一个变量,我们通过模板中的插值来使用它(stackblitz demo):
组件类:
@Component({...})
export class AppComponent {
prop = 'Test';
...
}
Run Code Online (Sandbox Code Playgroud)
模板:
<p>{{ this.prop }}</p>
<p>{{ prop }}</p>
Run Code Online (Sandbox Code Playgroud)
为什么在 Angular 中可以this在模板中使用关键字而没有任何警告/错误(即使在 AOT 模式下)?它的背后是什么?
编辑
根据答案中的注释:this 指的是为其呈现模板的组件本身。但我也可以创建一个模板变量并使用this以下方法访问它:
<input #inp> {{ this.inp.value }}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们inp在组件类中没有变量,我仍然可以使用{{this.inp...}}. 魔法?
我将从我的组件在托管组件上的使用开始:
<my-templated-component [content-template]="contentTemplate">
</my-templated-component>
<ng-template #contentTemplate>
<h3>Hello There</h3>
<div>Lorem ipsum..... </div>
<custom-component></custom-component>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
现在,我的组件的 .ts:
class MyTemplatedComponent implements AfterContentInit {
@Input('content-template') contentTemplate: TemplateRef<any>;
@ContentChildren(CustomComponent) customComponentQueryList: QueryList<CustomComponent>;
...
ngAfterContentInit() {
// Length is 0 at this point. My component didn't find the component I want
console.log(`There are ${this.customComponentQueryList.length} items`);
this.customComponentQueryList.changes.subscribe((s) => {
/* This is not called. This is where I would have wanted to find
my CustomComponent to perform a task that requires its presence.
*/
});
}
}
Run Code Online (Sandbox Code Playgroud)
这是 …
angular ×10
html ×3
data-binding ×1
javascript ×1
ng-switch ×1
ngfor ×1
templates ×1
typescript ×1