正在ES6脂肪箭头功能与角不兼容? [英] Are ES6 fat arrow functions incompatible with Angular?
问题描述
下面是在我的角度code正常ES5功能,它的工作原理:
app.run(函数($ templateCache){$ templateCache.put('/一些','东西')});
我想将它转化成脂肪ES6箭头功能
app.run($ templateCache => $ templateCache.put('/一些','东西'));
但它给错误
未捕获的错误:[$喷油器:unpr]未知提供商:'/ some'Provider< - '/一些
http://errors.angularjs.org/1.4.6/$injector/unpr?p0='%2Fsome'Provider%20%3C-%20'%2Fsome
REGEX_STRING_REGEXP @ angular.js:68
(匿名函数)@ angular.js:4287
@的getService angular.js:4435
(匿名函数)@ angular.js:4292
@的getService angular.js:4435
调用@ angular.js:4467
(匿名函数)@ angular.js:4297
@的forEach angular.js:336
createInjector @ angular.js:4297
doBootstrap @ angular.js:1657
引导@ angular.js:1678
angularInit @ angular.js:1572
(匿名函数)@ angular.js:28821
触发@ angular.js:3022
事件处理程序@ angular.js:3296
的ES6脂肪箭头功能与角抵触?
编辑:我想也许角是不能够推断出名称 $ templateCache
等无法注入,但后来我登录它控制台,它确实表明它正确:
app.run($ templateCache =>的console.log($ templateCache));
// =>
//对象{}
//破坏:功能()
//获取:功能(键)
//信息:功能()
//放:功能(键,值)
//删除:功能(键)
//的removeAll:功能()
// __proto__:对象
正确的。 您AngularJS的版本不带箭头的功能使用的兼容$注射器。
这主要是由于AngularJS 1.4.6利用(功能)的ToString
,不与启动功能($ C的$ C>为箭头的功能,至少在Firefox中:
> VAR一个=()=>五
起作用的()
> a.toString()
()=大于5//不是功能的(){返回5;}
AngularJS支持从箭头符号1.5.0起。
Here's a normal ES5 function in my Angular code which works:
app.run(function($templateCache){ $templateCache.put('/some','thing') });
I wanted to convert it to ES6 fat arrow function
app.run($templateCache => $templateCache.put('/some','thing'));
but it gives the error
Uncaught Error: [$injector:unpr] Unknown provider: '/some'Provider <- '/some'
http://errors.angularjs.org/1.4.6/$injector/unpr?p0='%2Fsome'Provider%20%3C-%20'%2Fsome'
REGEX_STRING_REGEXP @ angular.js:68
(anonymous function) @ angular.js:4287
getService @ angular.js:4435
(anonymous function) @ angular.js:4292
getService @ angular.js:4435
invoke @ angular.js:4467
(anonymous function) @ angular.js:4297
forEach @ angular.js:336
createInjector @ angular.js:4297
doBootstrap @ angular.js:1657
bootstrap @ angular.js:1678
angularInit @ angular.js:1572
(anonymous function) @ angular.js:28821
trigger @ angular.js:3022
eventHandler @ angular.js:3296
Are ES6 fat arrow functions incompatible with Angular?
EDIT: I thought perhaps Angular isn't able to infer the name $templateCache
and so unable to inject it but then I logged it to console and it does show it correctly:
app.run($templateCache=>console.log($templateCache));
// =>
// Object {}
// destroy: function()
// get: function(key)
// info: function()
// put: function(key, value)
// remove: function(key)
// removeAll: function()
// __proto__: Object
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.
这篇关于正在ES6脂肪箭头功能与角不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!