Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2 template use console.log

Tags:

angular

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() {} } 
like image 541
emvidi Avatar asked Nov 15 '16 10:11

emvidi


People also ask

How to use console log Angular?

First, you create a simple log service class to log messages using console. log() . Next, you add some logging levels so you can report on debug, warning, error, and other types of log messages. Then you create a generic logging service class to call other classes to log to the console, local storage, and a Web API.

Can I use script tag in Angular template?

To eliminate the risk of script injection attacks, Angular does not support the <script> element in templates. Angular ignores the <script> tag and outputs a warning to the browser console. For more information, see the Security page.

Can I use console log in TypeScript?

TypeScript TopicsUsing console. log, we can write content to the web debug console. This is useful for debugging and logging.

Can you console log in HTML?

The console. log() method in HTML is used for writing a message in the console. It indicates an important message during testing of any program.


2 Answers

You can't access globals, statics, ...

You can only access properties of the component the view belongs to.

You can add a

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).

For debugging I prefer

 {{item | json}}  
like image 71
Günter Zöchbauer Avatar answered Sep 22 '22 11:09

Günter Zöchbauer


Better way to do it :

This way you can access all the console properties on template side


Component side :

export class AppComponent  {   console = console; } 

Template Side :

{{ console.log('----------------> Logging') }} {{ console.warn('----------------> Warning') }} {{ console.error('----------------> error') }} 

WORKING DEMO

like image 32
Vivek Doshi Avatar answered Sep 18 '22 11:09

Vivek Doshi