0

I have following code in AngularJS. I am unable to access the original method parameter of 'preference' within the then function of 'updatePreferenceComplete'.

What is the way to access the original parameter of 'preference' in the above then function?

        function updatePreference(preferenceId, preference) {
        var prefId = angular.isNumber(preferenceId) ? preferenceId : 0;
        logger.info('updatePreference: fetching; preferenceId: ' + prefId);

        var route = userPreferenceConstants.endPoints.PREFERENCE_BY_ID
            .replace('{prefId}', prefId);

        return $http.put(route, preference)
            .then(updatePreferenceComplete)
            .catch(function (message) {
                exception.catcher('XHR failed for updatePreference')(message);
            });

        function updatePreferenceComplete(response) {
            //cannot access preference parameter
            logger.info('updatePreference: complete');
            return response.data;
        }
    }
15
  • Can you replicate in fiddle? Commented May 1, 2015 at 21:38
  • 2
    Which browser are you testing this on? I'm getting the correct variable from func alert. Commented May 1, 2015 at 21:44
  • 2
    Preference should be accessible in this context... and your variable shows up in the alert for me as well in the fiddle. Commented May 1, 2015 at 21:52
  • 2
    @Sunil No, I hard coded 'variable from func' into it. Commented May 1, 2015 at 22:03
  • 1
    @Sunil Yep, that's why it's weird that you don't see it in your code. Commented May 1, 2015 at 22:06

1 Answer 1

2
    return $http.put(route, preference)
        .then(function(response) {
            updatePreferenceComplete(response, preference);
        })
        .catch(function (message) {
            exception.catcher('XHR failed for updatePreference')(message);
        });

    function updatePreferenceComplete(response, preference) {
        // now you can access preference, because it's passed as argument to the function
        logger.info('updatePreference: complete');
        return response.data;
    }
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.