lag*_*lex 13 javascript angularjs ecmascript-6 angularjs-injector arrow-functions
这是我的Angular代码中的普通ES5函数,它可以工作:
app.run(function($templateCache){ $templateCache.put('/some','thing') });
Run Code Online (Sandbox Code Playgroud)
我想将它转换为ES6箭头功能
app.run($templateCache => $templateCache.put('/some','thing'));
Run Code Online (Sandbox Code Playgroud)
但它给出了错误
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
Run Code Online (Sandbox Code Playgroud)
ES6箭头功能是否与Angular不兼容?
编辑:我想也许Angular无法推断名称$templateCache,因此无法注入它,但后来我将其记录到控制台,它确实显示正确:
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
Run Code Online (Sandbox Code Playgroud)
正确.您的AngularJS版本与使用$ injector的箭头函数不兼容.
这主要是因为AngularJS 1.4.6利用的(Function).toString,不与启动function(为箭头的功能,至少在Firefox:
>var a = () => 5
function a()
>a.toString()
"() => 5" // not "function a() {return 5;}"
Run Code Online (Sandbox Code Playgroud)
AngularJS支持从1.5.0开始的箭头符号.
| 归档时间: |
|
| 查看次数: |
4023 次 |
| 最近记录: |