Javascript 如何在Angularjs中将字符串转换为对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38180525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert string to object in Angularjs
提问by Ehsan Ali
回答by Deblaton Jean-Philippe
You can do it with angular.fromJson()
in your sample, it would have been $scope.tmp = angular.fromJson($scope.text);
在你的样本中,它本来是 $scope.tmp = angular.fromJson($scope.text);
The difference between JSON.Parse()
and angular.fromJson
, is that angular will check to make sure a string is provided. If it is already an object, it will return the same object.
JSON.Parse()
和angular.fromJson
,之间的区别在于angular 将检查以确保提供了一个字符串。如果它已经是一个对象,它将返回相同的对象。