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

前端 未结 3 1739
有刺的猬
有刺的猬 2020-12-13 19:53

Please see my following jsFiddle example where I am trying to push an Angular.js object into a JSon representations using angular.toJson. What I get is just \"$SCOPE\" as th

3条回答
  •  星月不相逢
    2020-12-13 20:17

    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:

     
    

    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.

提交回复
热议问题