0

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!

13
  • what do you mean by 'it only updates the value in the pair'? whats your desired outcome? Commented Jul 3, 2015 at 21:17
  • i mean, when it updates it becomes 'data': {"json":{"bit":[{"keyname":"key","valuename":value},{"keyname":"key","valuename":value},{"keyname":"key","valuename":value}... and so on id like it to scrap whats already there and only be left with my json as the object Commented Jul 3, 2015 at 21:18
  • do you have a jsfiddle? Commented Jul 3, 2015 at 21:19
  • not of my little monster, but i got the service part from jsfiddle.net/b2fCE/1 Commented Jul 3, 2015 at 21:23
  • What is your result now? And what are you trying to achieve? Still don't get it Commented Jul 3, 2015 at 21:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.