Angular.toJson / JSON.stringify invalid values?

I have an object attached to $scopewhich I want to serialize to JSON. The object was configured with data binding, so there are input fields using ng-modeland what not. When you try to call the angular.toJsonvalues ​​are not updated.

It is strange that I realized that my values ​​were not updated as I thought. So I launched some calls console.logfor simplicity, but the values ​​from console.logare correct, but they are not for JSON conversion? I tested it with JSON.stringify, but the results were the same. The code:

// This looks fine
console.log('Data:', $scope.obj);
var temp = angular.toJson($scope.obj);
// This looks fine as well...
console.log('Data:', $scope.obj);
// Yet the JSON string isn't correct and contains old data?
console.log('Data:', temp);

Is this the Angular issue I am facing that is related to data binding? Or is something else going on?

+4
1

var temp = angular.toJson($scope.obj); , temp, , :

var temp;
$scope.$watch('obj', function(newVal) {
    temp = angular.toJson(newVal);
    console.log('Data:', temp);
});

, , console.log() console.log(). console.log() string/number/boolean. console.log($scope.obj), $scope.obj - ( ajax, $scope.$apply()), ( DOM ).

+5

All Articles