bronze
bronze

Reputation: 79

AngularJS/Javascript - How can I replace an entire object with JSON

I'm trying to update an objectValue with a JSON callback.
As per the code below, if i leave objectValue.data it only updates the value in the pair. I've tried taking the .data off but then it has no response.

Is there a way i can overwrite completely objectValue with the JSON request i get?

app.service('sharedProperties', function() {
    var objectValue = {
        'data': 'Click to change'
    };
    return {
        setObject: function(value) {
            objectValue.data = value;
        },
        getObject: function() {
            return objectValue;
        }
    };
});

cheers!

Upvotes: 0

Views: 259

Answers (1)

hogan
hogan

Reputation: 1561

I don't see a problem with this. I tried:

....    
// service
    setObject: function(value) {
        objectValue = value;
    },


// and in the controller
app.controller('myController', function($scope, sharedProperties) {
   $scope.stringValue = sharedProperties.getString;
   $scope.objectValue = sharedProperties.getObject();
   $scope.setString = function(newValue) {
       $scope.objectValue.data = newValue;
       sharedProperties.setObject(newValue);
       alert(sharedProperties.getObject());
   };
});

And it alerts's the object. Maybe there is something wrong with another part of your code. Try to log/alert step by step and see what happens.

Upvotes: 1

Related Questions