最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why we can not use directly console.log in Angular? - Stack Overflow

programmeradmin6浏览0评论

Say I have a variable and I want to see its value in the console. But in Angular I can not just write {{ console.log(variable) }} in my template. I must recreate this function in my class, like:

testponent.ts:

log(val) { console.log(val) }

Then I can get the value:

testponent.html:

{{ log(variable) }}

So why I cannot just write {{ console.log(variable) }}?

Say I have a variable and I want to see its value in the console. But in Angular I can not just write {{ console.log(variable) }} in my template. I must recreate this function in my class, like:

test.ponent.ts:

log(val) { console.log(val) }

Then I can get the value:

test.ponent.html:

{{ log(variable) }}

So why I cannot just write {{ console.log(variable) }}?

Share Improve this question edited Jul 11, 2017 at 6:24 Jadeye 4,3494 gold badges53 silver badges67 bronze badges asked Jul 11, 2017 at 5:20 Julius DzidzevičiusJulius Dzidzevičius 11k11 gold badges41 silver badges84 bronze badges 10
  • 1 you can follow this answer in stackoverflow same question in stackoverflow – Sameera Prasad Jayasinghe Commented Jul 11, 2017 at 5:23
  • @Claies - that was probably the best answer. Thanks! If you will provide the answer I will accept it – Julius Dzidzevičius Commented Jul 11, 2017 at 5:48
  • 1 Hi @FromZeroToHero - I have added in the correct answer below (you can in fact do what you want) - enjoy :) – NightCabbage Commented Jul 11, 2017 at 5:56
  • Is this question about Angular 1.x or Angular 4? – Günter Zöchbauer Commented Jul 11, 2017 at 6:25
  • 1 @Günter Zöchbauer, yes, I totally agree, I just meant that this is an example of how lack of knowledge burden our world. But you are totally right - I ignored the fact that people might not know it (even if I think it is obvious) and didn't note this fact on purpose and unconsciously. Next time I will try to provide more details just because I know that users usually lack this kind of info. Good that I know what they lack... – Julius Dzidzevičius Commented Jul 11, 2017 at 7:30
 |  Show 5 more ments

4 Answers 4

Reset to default 5

So in Angular (2/4), the only things accessible in the template are things that fall within the scope of your corresponding ponent's class - ie. anthing you could say "this.blah" in the ts.

So, you can in fact do what you propose, but you need to add console into the scope of your ponent:

@Component({
    selector: 'my-ponent',
    template: `This is my template {{console.log(variable)}}`
})
export class MyComponent {
    console = console;
    variable = 'yay';
}

(the key line here is console = console;)

And then this will output 'variable' with the text 'yay' to the console - the catch is that it will do that every time the expression is rendered... which will often be many times. So it's not particularly good practice, but if you're just debugging it could be ok.

Q: Why I cannot just write {{ console.log(variable) }}?

A: Short answer. Because the build-in functionality console is not part of the controller's $scope object.

This is because console.log() is a part of global window object and window object cannot be directly accessed in templates.

Whenever the angular engines finds the interpolation string {{}} it uses the built-in interpolation service that basically returns the scope associated with the expression and evaluates the variables used in that scope with respect to this returned scope. The template has access to only those properties that are in its scope. window object does not belong to the scope of ponent/controller hence can't be used directly.

Hope it clears your confusion.

you should use a function

ex.

$scope.log2=function(param){
   console.log(param);
}

then use function in view

{{log2(variable)}}
发布评论

评论列表(0)

  1. 暂无评论