我的cordova android 项目(angularjs+ionic)中遇到一个奇怪的问题,当我在android studio 中运行项目时,我的js 文件上会出现此错误。
Uncaught SyntaxError: Unexpected token {, http://192.168.43.211:8100/templates/todo/js/controllers.js, Line: 184
但是当我在 google chrome 中运行相同的文件时,我会得到结果并且不会显示错误
这是错误消息的代码块
$scope.todos = $scope.todos.map(({text, status,note,create_date,to_do_id}) => {
var ch;
if(status=='1') ch=true; else ch=false;
console.log(ch);
return {
text,
flag:ch,
note:note,
to_do_id:to_do_id,
create_date:create_date,
status:status
};
});
当我隐藏上面的代码块时,android studio 控制台中不会显示错误,但我需要上面的代码块来完成功能。
问题是什么
最佳答案
主要解决方案
您确定您的手机浏览器支持JS箭头表达式吗?它们是在 ECMAScript 6 中引入的,因此某些浏览器可能仍然不支持它们。尝试将箭头表达式更改为匿名函数:
$scope.todos = $scope.todos.map(function(record) {
var ch;
if (record.status == '1') ch = true; else ch = false;
console.log(ch);
return {
text: record.text,
flag: ch,
note: record.note,
to_do_id: record.to_do_id,
create_date: record.create_date,
status: record.status
};
});
<小时/>
替代解决方案
如果上面的代码仍然不起作用,您可以随时创建自己的 map 函数:
function _arrayMap(array, callback) {
var newArray = [];
for(var i = 0; i < array.length; i++) {
newArray.push(callback(array[i]));
}
return newArray;
}
并以类似的方式使用它:
$scope.todos = _arrayMap($scope.todos, function(record) {
var ch;
if (record.status == '1') ch = true; else ch = false;
console.log(ch);
return {
text: record.text,
flag: ch,
note: record.note,
to_do_id: record.to_do_id,
create_date: record.create_date,
status: record.status
};
});
关于javascript - Angular js :unexpected token error shown in android studio console, 但在浏览器中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37633675/