AngularJS angular.toJson()函数用法介绍

2021年3月11日17:12:27 发表评论 623 次浏览

的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 >

输出如下:

点击前:

toJSON

点击后:

toJSON

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: