2016-06-11 86 views
0

我开始学习AngularJS,但我偶然发现了一个奇怪的行为,我不明白(()=> {})表示法与(function(){})的等价位置。奇怪的AngularJS初学者行为

我的index.html:

<!DOCTYPE html> 
<html ng-app="gemStore"> 
    <head> 
    <title>AngularJS Store</title> 
    <script src="./angular.min.js"></script> 
    <script src="./app.js"></script> 
    </head> 
    <body> 
    <div ng-controller="StoreController as store"> 
     <h1>{{store.product.name}}</h1> 
     <h2>${{store.product.price}}</h2> 
     <p>{{store.product.description}}</p> 
    </div> 
    </body> 
</html> 

我app.js(关闭剥离用于调试)。

var app = angular.module('gemStore', []); 

app.controller("StoreController", function() { 
    this.product = gem; 
}); 

var gem = { 
    name: 'Dodecahedron', 
    price: 2.95, 
    description: '. . .' 
};  

在app.js,如果我改变

app.controller("StoreController", function() { 
    this.product = gem; 
}); 

app.controller("StoreController",() => { 
    this.product = gem; 
}); 

我的页面不再显示创业板信息(只是空白和美元符号保留)。

有人可以解释为什么会发生这种情况?

AngularJS版本:v1.5.6
歌剧版本:37.0.2178.54

+0

我不知道,如果角允许'()=>'符号。有没有你使用过'()=>'的代码,它能正常工作吗? –

+0

你试过用babel或webpack编译你的代码吗?并非所有浏览器都支持ES6 –

+1

箭头函数具有隐式'this'绑定,这意味着箭头函数内的this值的值与箭头函数范围内的this值相同被定义为! - [来源](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) –

回答