我有以下Ecma-Script-6的代码 template literals
let person = {name: 'John Smith'};
let tpl = `My name is ${person.name}.`;
let MyVar="My name is "+ person.name+".";
console.log("template literal= "+tpl);
console.log("my variable = "+MyVar);
Run Code Online (Sandbox Code Playgroud)
输出如下:
template literal= My name is John Smith.
my variable = My name is John Smith.
Run Code Online (Sandbox Code Playgroud)
这是小提琴.我试着寻找确切的差异但找不到它,我的问题是这两个陈述有什么区别,
let tpl = `My name is ${person.name}.`;
Run Code Online (Sandbox Code Playgroud)
和
let MyVar = "My name is "+ person.name+".";
Run Code Online (Sandbox Code Playgroud)
我已经能够在这里MyVar连接字符串person.name了,那么使用模板文字的场景是什么?
我想使用ngForAngular 2将 Firebase 查询的结果绑定到我的模板。这很容易在下面实现。
成分:
export class FeaturedThreadsComponent {
threads: FirebaseListObservable<any[]>;
qualitySubject: Subject<any>;
constructor(af: AngularFire) {
this.qualitySubject = new Subject();
this.threads = af.database.list('/threads', {
query: {
orderByChild: 'quality',
endAt: 5
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
模板:
<div *ngFor="let thread of threads | async">
{{thread.title}}
</div>
Run Code Online (Sandbox Code Playgroud)
但是如果我想使用ngFor嵌套在模板中的另一个指令来列出子对象的键......
<div *ngFor="let thread of threads | async">
{{thread.title}}
<div *ngFor="let participant of thread.participants">
{{participant.$key}}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我收到控制台错误, Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports …