Are ES6 arrow functions incompatible with Angular?

后端 未结 2 1660
半阙折子戏
半阙折子戏 2020-12-17 16:43

Here\'s a normal ES5 function in my Angular code which works:

app.run(function($templateCache){ $templateCache.put(\'/some\',\'thing\') });

相关标签:
2条回答
  • 2020-12-17 17:03

    I tried another variation which worked: (x)=>… (instead of x=>…)

    app.run(($templateCache) => $templateCache.put('/some','thing'));
    

    I guess it needs parentheses for some reason

    0 讨论(0)
  • 2020-12-17 17:21

    Correct. Your version of AngularJS is not compatible with arrow functions that make use of $injector.

    This is mainly because AngularJS 1.4.6 makes use of (Function).toString, which does not start with function( for arrow functions, at least in Firefox:

    >var a = () => 5
    function a()
    >a.toString()
    "() => 5"  // not "function a() {return 5;}"
    

    AngularJS supports the arrow notation from 1.5.0 onwards.

    0 讨论(0)
提交回复
热议问题