2

I'm testing the following code in my AngularJs application, it works fine in Firefox, but IE11 throws a syntax error for the arrows:

myApp.run((EventTitle, moment) => {
  EventTitle.weekView = event => `${moment(event.startsAt).format('hh:mm')} event.title`;
});

What would be the syntax that would work for IE?

user3489502
  • 3,451
  • 9
  • 38
  • 66
  • It's not supported in IE11. [This](http://caniuse.com/#search=%3D%3E) is a pretty good resource for what is and is not supported by various browsers – Matt Burland Dec 08 '16 at 17:49
  • 2
    No, arrow functions aren't supported in IE. Use [Babel](https://babeljs.io/) to compile ES6 code to ES5. – 4castle Dec 08 '16 at 17:50
  • Wow nice, thanks for the link to the Babel tool, didn't even know such a tool existed. It work now. Thanks – user3489502 Dec 08 '16 at 18:08

1 Answers1

3

IE11 doesn't support several ES6 features, including lambda functions and template literals.

A rough equivalent that should work is:

myApp.run(function(EventTitle, moment) {
  EventTitle.weekView = function(event) {
     return moment(event.startsAt).format('hh:mm') + " " + event.title;
  };
});

However, there are some ways in which arrow functions do not work in the same way as regular functions. You'll need to watch for the use of this keywords, for example.

Alternatively, if you use a transpiler like Babel (or a transpiled language like TypeScript), you can use newer language features like your original code, and automatically generate JavaScript code that will run in browsers that don't support those features.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315