javascript - 与打印时的静态数据相比,在控制台上打印的动态数据显示不同
问题描述
我正在将带有逗号分隔值的字符串从弹簧控制器返回到角度控制器,如下所示。
app.controller('myListController', ['$scope', function ($scope) {
//get data dynamically , logic..
$scope.myList = response;
console.log("$scope.myList " + $scope.myList); // "one,two","three","four","five,eight,nine"
//here i want to print as one,two,three,four,five,eight,nine
//convert string to array with comma separated values
var array_list = JSON.parse($scope.myList);
console.log("array_list :: " + array_list);
}]);
上面的代码抛出错误
angular.min.js?dummy=0.6014859345541128:119 SyntaxError: Unexpected token , in JSON at position 15
at JSON.parse (<anonymous>)
相同的代码,当替换为静态代码时,它按预期工作。例子:
$scope.myList = ["one,two","three","four","five,eight,nine"];
console.log("$scope.myList static content:: " + $scope.myList); //one,two,three,four,five,eight,nine
上面的任何输入?
解决方案
正如我在您的 JSON.stringify 结果中看到的,您的响应字符串必须是这个值:
'"one,two","three","four","five,eight,nine"'
如果是,您可以使用简单的正则表达式来获得您想要的结果:
var result = $scope.myList.replace(/"/g, ""); //"one,two,three,four,five,eight,nine"
如果这不起作用,请分享您的 $scope.myList 的确切值
推荐阅读
- c# - 500 从 .net core 2.1 调用标量数据库函数时出错
- reactjs - 将 FilterText 或 Search 输入实现到 reactjs 应用程序
- r - R人口统计学包消息长度中的read.demogdata
- javascript - 如何让 IFrame 一次重绘所有内容
- dynamics-crm - 用于合并创建线索的插件
- mysql - 使用 MYSQL 提取文本列的一部分
- bash - bash 解析空格分隔的文本文件
- apache - 当存在同名文件时,Htaccess 正在匹配目录
- excel - Vlookup 并求和一个包含逗号分隔值的单元格
- vb.net - 如何使用 DataGridView.AutoGenerateColumns = False 创建我自己的列