JSON JavaScrip grabbing value

#1

Can anyone help me here? I’m trying to grab a value from json and place it into my controller and having a brain fart here.

The value 9 it’s trying to place in $scope.value in the controller

JSON

{"membership":"9"}

JS. Services

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

JS. Controller

 .controller('MembershipCtrl', function ($scope, $interval, getMembership) {
          $scope.value = getMembership.getMembership(alart(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
            }
        })

#2

Hi,

Service getMembership has one method inside it which expect one parameter (in your case userid).
If i understand well you need something like this:

Controller:

For example you wanna fetch user with id: 10,

var userId = 10;
$scope.value = getMembership.getMembership(userId);

Return value from service will be in this case user Object with id of 10.
So, you need a promise inside controller:

.controller('MembershipCtrl', function($scope, $interval, getMembership) {
    // Fetch user with id of 10
    var userId = 10;
    getMembership.getMembership(userId).success(function(data) { // Data is a fetched USER OBJECT.
            $scope.value = data.membership; // {"membership": "9"} => $scope.value = 10;
            // do something
        },
        function errorCallBack(data) {
            // error handling 
            console.log("error" + data)
        }).finally(function() {
        // do something
    })
})

Controller with your code:

.controller('MembershipCtrl', function($scope, $interval, getMembership) {
    // Fetch user with id of 10
    var userId = 10;
    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
            }
        },
        function errorCallBack(data) {
            // error handling 
            console.log("error" + data)
        }).finally(function() {
        // do something
    })
})