如何在$http中传递数组对象.在angularjs中

How to pass the array object in $http.get method in angularjs

本文关键字:数组 对象 angularjs http      更新时间:2023-09-26

我想在$http.get()中传递数组对象。以前所有的信息都存储在data.json文件中,但我不想使用文件。要在控制器中定义对象的Array为$scope.data。请找到DEMOhttp://plnkr.co/edit/X5ZC3UGey4Zjos7W01U1?p=preview

工作演示http://plnkr.co/edit/o6EeKnC8oFEq3GCRsQgp?p=preview这里我们使用data.json。我想在数据中定义数据。请告诉我如何添加

.controller('QuestionCtrl', ['$scope', '$http', function ($scope, $http) { 
  $scope.questions = [];
  $scope.data =   {
  "questions": [
    {
      "question": "Qw1",
      "answerswers": [
        {
          "answerswers": "An1",
          "value": 25
        },
        {
          "answerswers": "An2",
          "value": 50
        },
        {
          "answerswers": "An3",
          "value": 75
        },
        {
          "answerswers": "An4",
          "value": 100
        }
      ]
    },
    {
      "question": "Qw2",
      "answerswers": [
        {
          "answerswers": "An1",
          "value": 25
        },
        {
          "answerswers": "An2",
          "value": 50
        },
        {
          "answerswers": "An3",
          "value": 75
        },
        {
          "answerswers": "An4",
          "value": 100
        }
      ]
    }
  ]
}
  $http
    .get(JSON.stringify($scope.data))
    .then(function(response){
      $scope.questions = response.data.questions;
    });
}]) 

我正在使用这个,它对我来说效果很好

$http.get('url', {params: yourArray}).then(function (result) {
  //do sth with data
});

HTTP GET请求不能包含要发送到服务器的数据。您可以向请求添加查询字符串。$http选项为params

$http({
    url: 'example.com', 
    method: "GET",
    params: {...}
 });

如果数据直接在控制器中可用,则不需要使用$http.get()。

You can use $scope.data instead of $scope.questions. ie in your html file use
{{data.questions[0].question}}
<h2>answer 1</h2>
<p>{{data.questions[0].answers[0].value}}</p>
<p>{{data.questions[0].answers[0].answers}}</p>

我已经尝试过这种方式,它工作得很好:例如在你的控制器中你有任意数组比如arr变量

var data = Array;
    data['Id'] = "1";
    data['Name'] ="Test User";
    data['UserName'] = "test_user";
    data['Email'] = "test@gmail.com";
    data['Address'] = "Test Address";
    data['Action'] = 'Create';

要传递这个数组,你可以像这样使用

$scope.users = data;

控制器在视图页中发送一个数组作为变量users你可以这样写

ng-repeat="user in users"

AngularJS有$http服务,用来异步获取数据。所以它可以返回任何类型的数据(变量,数组,字符串)。

最好将数据存储在公共位置。让我来展示一下用于在控制器之间共享数据的factory模式。

首先,您应该创建yourFactory.js文件并编写以下代码:

(function () {    
    var yourFactory = function () {
        var persons = [
                {
                    id: 1,                    
                    name: 'Jon',                    
                },
                {
                    id: 2,                    
                    name: 'Ben',                    
                },
                {
                    id: 3,                    
                    name: 'Joseph',                    
                },
                {
                    id: 4,                    
                    name: 'Adam',                    
                }
        ];
        var factory = {};
        factory.getPersons = function () {
            return persons;
        };
        return factory;
    };
    angular.module('personsApp').factory('personsFactory', yourFactory);
}());

和一些控制器可以使用数据(只需为以下控制器创建新的.js文件):

(function()
{    
    debugger;
    var yourController=function($scope, yourFactory) { 
                 /*alert('a1');*/
                 debugger;                 
                 function init() {
                    debugger;
                    $scope.yourData=yourFactory.getPersons();
                 }        
                 init();   
             };    

    yourController.$inject=['$scope', 'yourFactory'];   
    angular.module('yourAppl').controller('yourController', yourController);
}());