angular - 箭头运算符 VS 在函数中传递参数:有什么区别?
问题描述
//component.ts放入箭头操作符和放入函数有
什么区别。i
i
this.service.getuser().subscribe(i => {
this.myuserList = i;
});
// 如下图,下面的代码数据数据没有绘制在html页面中
this.service.getUserList().subscribe(function(i : any){
this.myuserList = i;
});
// 也放上html代码供参考
//component.html
<tr *ngFor = 'let p of myuserList'>
<td>{{p.username}}</td>
<td>{{p.address.suite}}</td>
</tr>
解决方案
在使用function
(ES5 语法)时,this 关键字将引用函数定义。在使用胖箭头=>
(ES6 语法)时,this 关键字指的是外部函数。
function fun1() {
let someVar = 1;
someFunctionWithCallback(function() {
// this keyword referes to function passed to callback
// cannot access outside variables here
console.log(someVar) // undefined
});
someFunctionWithCallback(() => {
// this keyword referes to outside function
console.log(someVar) // will be accessible e.g. 1
});
}
推荐阅读
- python - 如何检查 URL SSL 是否让我们以编程方式加密?
- pandas - 多索引熊猫图中时间序列的 X 刻度标签
- php - hex2bin 与 PHP 中的 PASSWORD_DEFAULT 算法的密码哈希值相等吗?
- django - 将 DRF api 与 Vanilla Django 一起使用的任何好的资源?
- r - Inner_join 和过滤,消失的观察
- c++ - 如何在此请求中使用 libcurl 并获取 json server answer c++?
- python - 如何在二维数组中的第一个循环之后打印
- python - 如何在 discord.py 中创建问答命令
- matlab - 在MATLAB中使用sendmail发送邮件,带有/指向签名
- java - 在 BitcoinJ Java 中从 WIF 生成 P2SH 比特币地址