的angular.toJson()函数在AngularJS中用来将javascript对象序列化为JSON格式的字符串。它使用javascript对象并返回JSON字符串。
语法如下:
angular.toJson(object)
例子:
<!DOCTYPE html>
< html >
< head >
< script src =
"//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js" >
</ script >
< title >
angular.toJson()
</ title >
</ head >
< body ng-app = "app"
style = "text-align:center" >
< h1 style = "color:green" >
lsbin
</ h1 >
< h2 >
angular.toJson()
</ h2 >
< div ng-controller = "geek" >
< button ng-click = "showAlert()" >
Click it!</ button >
</ div >
< script >
var app = angular.module("app", []);
app.controller('geek', ['$scope', function($scope) {
$scope.showAlert = function() {
var string = {
'Name': 'Quick sort', 'Type': 'sorting'
};
alert(angular.toJson(string));
}
}]);
</ script >
</ body >
</ html >
输出如下:
点击前:
点击后: