角2模板使用console.log [英] angular 2 template use console.log
本文介绍了角2模板使用console.log的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在嵌入式模板中使用console.log,但是找不到任何方向.
I would like to use the console.log inside the inline template but can't find any directions.
@Component({
selector:"main",
providers: [ItemService],
template:`
<ul>
<li *ngFor="let item of items">
{{console.log(item)}} <----- ???
<p>{{item.name}}</p>
</li>
</ul>
`
})
export class HomeComponent {
private items: Array<ItemModel>;
constructor() {}
}
推荐答案
您无法访问全局变量,静态变量,...
You can't access globals, statics, ...
您只能访问视图所属组件的属性.
You can only access properties of the component the view belongs to.
您可以添加
log(val) { console.log(val); }
添加到您的组件并像使用
to your component and use it like
{{log(item)}}
但是要做好准备使其经常记录(每次运行更改检测).
but be prepared this to be logged quite often (every time change detection runs).
我更喜欢调试
{{item | json}}
这篇关于角2模板使用console.log的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文