How to pass a value to an AngularJS $http success callback

前端 未结 5 934
旧时难觅i
旧时难觅i 2020-12-31 04:54

In my AngularJS application I am doing the following

$http.get(\'/plugin/\' + key + \'/js\').success(function (data) {
    if (data.length > 0) {
                 


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-31 05:11

    Solution 1:

    $scope.key = key;
    $http.get('/plugin/' + key + '/js').success(function (data) {
        if (data.length > 0) {
            console.log(data, $scope.key);
        }
    });
    

    Solution 2 (Updated per Jim Hong's observation in his answer):

    $http.get('/plugin/' + key + '/js').success((function(key) {
        return function(data) {
            console.log(key, data);
        }
    })(key));
    

提交回复
热议问题