How to use angular.toJson on a angular controller or scope

likestoski提出了一个问题:How to use angular.toJson on a angular controller or scope,或许与您遇到的问题类似。

回答者pkozlowski.opensource给出了该问题的处理方式:

I can see that you are coming from the jQuery world, but with angular.js things are getting much simpler, please check this jsFiddle: http://jsfiddle.net/pkozlowski_opensource/ASspB/1/

With angular.js you can attach events much, much simpler:

 <input type="button" ng-click="showJson()" value="Object To JSON" />

and then in your controller:

 $scope.showJson = function() {
    $scope.json = angular.toJson($scope.user);
}

In fact this could be done even easier with angular.js filters, check this jsFiddle: http://jsfiddle.net/pkozlowski_opensource/ASspB/2/ which has:

{{user | json}}

With angular.js you need to "unlearn" a bit some of the jQuery habits, but this is good since things get much, much easier most of the time.

希望本文对你有帮助,欢迎支持JavaScript中文网


公众号

欢迎 JavaScript 爱好者