如何在元素中声明动态 模板引用变量ngFor
?
我想使用ng-bootstrap中的popover组件,popover代码(带有Html绑定)如下所示:
<ng-template #popContent>Hello, <b>{{name}}</b>!</ng-template>
<button type="button" class="btn btn-secondary" [ngbPopover]="popContent" popoverTitle="Fancy content">
I've got markup and bindings in my popover!
</button>
Run Code Online (Sandbox Code Playgroud)
如何将这些元素包装在里面ngFor
?
<div *ngFor="let member of members">
<!-- how to declare the '????' -->
<ng-template #????>Hello, <b>{{member.name}}</b>!</ng-template>
<button type="button" class="btn btn-secondary" [ngbPopover]="????" popoverTitle="Fancy content">
I've got markup and bindings in my popover!
</button>
</div>
Run Code Online (Sandbox Code Playgroud)
嗯......任何想法?
我有一个结构:
type Human struct {
Head string `json:"a1"`
Body string `json:"a2"`
Leg string `json:"a3"`
}
Run Code Online (Sandbox Code Playgroud)
如何通过提供 JSON 标记名称来获取结构的字段名称?大概是这样的:
fmt.Println(getFieldName("a1")) // "Head"
fmt.Println(getFieldName("a2")) // "Body"
fmt.Println(getFieldName("a99")) // ""
func getFieldName(tag string) (fieldname string) {
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
我应该如何实现该getFieldName
功能?我在网上看了,好像需要用到这个reflect
包,嗯……有帮手吗?:)
article/:id
每次单击按钮时我都想重新加载页面,Angular 能够在:id
更改时重新加载页面,但是如果路由到 same :id
,则它会忽略更改。
export class DocumentComponent {
constructor(
private activatedRoute: ActivatedRoute,
private router: Router
) {}
ngOnInit() {
this.routeSubscription = this.activatedRoute.params.subscribe(params => {
this.reloadPage(+params['id']);
});
}
clickReload(id: number) {
this.router.onSameUrlNavigation = 'reload';
this.router.navigate(['article', id]);
}
}
Run Code Online (Sandbox Code Playgroud)
当 params 改变时,observableparams
将被触发,并被reloadPage()
执行。但是,当我尝试重新加载相同的params
Angular 时会忽略它,我该如何重新加载相同的param
?
article/123 --> article/456 [ok]
article/111 --> article/222 [ok]
article/666 --> article/666 [not ok, how?]
Run Code Online (Sandbox Code Playgroud)
我尝试使用onSameUrlNavigation
,但它不起作用......我做错了什么吗?
我有一个复杂的组件,其中包含许多 @Input() 和异步进程。我想优化组件,因此打算在我的组件上分析 Angular4 更改检测周期。
我可以知道如何识别Angular4 更改检测系统是否正在被触发,以及 DOM 是否正在重新呈现?
可能,使用console.log(...)
?
通常,我希望类似(在浏览器控制台中):
Change-detection running... no changes... 1
Change-detection running... no changes... 2
Change-detection running... no changes... 3
Change-detection running... no changes... 4
Change-detection running... changed
No DOM rendering
Change-detection running... no changes... 5
Change-detection running... no changes... 6
Change-detection running... changed
DOM re-rendered
Change-detection running... no changes... 7
Change-detection running... no changes... 8
Run Code Online (Sandbox Code Playgroud)
我如何实现这一目标?或者你们如何优化 Angular4 的性能?还有其他方法吗?
如何templateRef
从其他组件模板文件中使用?
我有BatmanComponent
,SpidermanComponent
还有一个JokerComponent
。其中一些具有类似的功能,因此我决定创建一个HumanComponent
, 并将所有可重用的 HTML 代码放在该文件中,如下所示:
注意:HumanComponent
永远不会使用,它只是一个包含所有默认模板的文件。
<!-- human.component.ts -->
<ng-template #eye>
Eyes: {{ size }}
</ng-template>
<ng-template #body>
Body: bust {{ x[0] }}, waist {{ x[1] }}, hip {{ x[2] }}
</ng-template>
Run Code Online (Sandbox Code Playgroud)
我可以知道如何注入这些模板(来自human.component.ts)并在batman.component.ts
.... 中使用它吗?
我知道我可以这样做:(仅当模板代码被复制粘贴到蝙蝠侠、蜘蛛侠和小丑HTML 文件中时)
<!-- batman.component.ts -->
<ng-container *ngTemplateOutlet="eye; context: contextObj"></ng-container>
<ng-template #eye> ... </ng-template> <!-- need to copy/paste here, and use it locally -->
Run Code Online (Sandbox Code Playgroud)
我可以知道如何导出 templateRef …
我可以知道如何time.time
在Postgresql中存储对象吗?
例如,SQL查询:
INSERT INTO "UserAccount" ("email", "login_time") VALUES ('human@example.com', 2017-12-12 00:58:26.9589451 +0800 +08 m=+1406.914186601)
我尝试使用loginTime := time.Now()
,它给出了Postgresql不太懂的时间格式,例如2017-12-12 00:58:26.9589451 +0800 +08 m = + 1406.914186601
但是,如果我尝试使用loginTime := time.Now().Format(time.RFC3339)
,并且编译器抱怨这loginTime
是一个string
,我需要它是time.time
类型.
我可以知道如何处理吗?
很抱歉提出新手问题,Golang和Postgres都是新手.:)
我正在使用Angular Material Dialog组件,我想传递一个可选的回调函数,并在用户单击OK button
. 我可以知道如何实施吗?
askUser(customData: any) {
openDialog() {
const dialogRef = this.dialog.open(AskDialog, {
data: customData
});
dialogRef.afterClosed().subscribe(isOK=> {
if (isOK && customData.hasOwnProperty('callback')) {
// ??? how to execute the "customData.callback"
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我希望我可以这样使用askUser()
:
function freeGift(gift: string) { /* ... */ }
function contactPolice(phone: number, email: string) { /* ... */ }
askUser({ // callback
displayText: 'Are you a superman?',
callback: freeGift('blue shirt')
});
askUser({ // different callback with …
Run Code Online (Sandbox Code Playgroud)