0

I'm trying to grab a value from json and place it into my controller and having some issue.

I'm looking to have the membership value 8 put into $scope.value = data.membership;

JS. Service call

.service('getMembership', function ($http, SERVER_URL) {
        return {
            getMembership: function (userid) {
                var url = SERVER_URL + "services/getMembership.php?userid=" + userid;
                return $http.get(url);
            }

        };
    })

The php will return the following, which in turn will be returned by javascript.

{"membership":"8"}

Now I'm trying to add this in my controller and call it out into a chart in $scope.value.

    .controller('AwardCtrl', function ($scope, $interval, getMembership) {
      var userId = 1;
      getMembership.getMembership(userId).success(function(data) { 
        $scope.value = data.membership;

        $scope.options1 = {
        animate : { enabled: true, duration: 1500, ease: 'bounce' },
         readOnly: true,
          size: 200,
          max: 30,
          subText: {
            enabled: true,
            text: 'DAYS LEFT',
            color: '#f2f2f2;',
            font: 'auto'
          },
          trackWidth: 40,
          barWidth: 25,
          trackColor: '#e6e6e6',
          barColor: '#81d8cf',
          dynamicOptions: true
        }
      });
    })

1 Answer 1

2

PHP will return a string, but does the javascript $http.get convert this to a JSON object automatically? If not, you can use

var actualobject = JSON.parse(data);
$scope.value = actualobject.membership;

to convert it. See: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

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

2 Comments

I'd consider using MDN as a better resource for Javascript Docs - JSON.parse() - see this explanation
You could use JSON.parse inside the success function, on the data parameter. What you've used above is parsing the angular promise.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.