如何使用$ http服务仅返回angularjs中的特定json对象值

时间:2014-06-12 11:59:56

标签: angularjs angularjs-scope

我想在json对象中检索特定值并将其分配给变量。在这里,我想将返回的语言值分配给数组变量。怎么做?

$http.get("http://localhost:81/Lhrms/public/languagesService").success(function(data){

   deferred.resolve(data);

});
    return deferred.promise;

返回以下数据:

[{"id":"39","languages":"test","created_at":"2014-06-12 10:29:17.000","created_by":"1","updated_at":"2014-06-12 10:29:17.000","updated_by":"1","is_active":"1"},{"id":"38","languages":"test","created_at":"2014-06-12 10:29:17.000","created_by":"1","updated_at":"2014-06-12 10:29:17.000","updated_by":"1","is_active":"1"}];

如何只返回返回数据中的语言。

2 个答案:

答案 0 :(得分:0)

为什么不成功?

return $http.get("Api/Controller/GetLanguages")
                .then(function(results) {
                     return results.data.languages;
                }

答案 1 :(得分:0)

你可以使用underscore.js和function pluck

$http.get("http://localhost:81/Lhrms/public/languagesService").success(function(data){
var languages = _.pluck(data, 'languages') ;
   deferred.resolve(languages );

});
    return deferred.promise;

或者您可以通过结果迭代并从语言中创建新数组

 $http.get("http://localhost:81/Lhrms/public/languagesService").success(function(data){
    var languages =[];

    angular.forEach(data, function(value){
       languages.push(value.languages);
    }
       deferred.resolve(languages );

    });
        return deferred.promise;