如何在$http中传递数组对象.在angularjs中
How to pass the array object in $http.get method in angularjs
我想在$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);
}());
相关文章:
- 将数组对象传递到struts2中的操作类
- 如何使用jquery返回php-json数组对象
- 从其名称获取javascript数组对象
- 值未与数组对象绑定
- 如何按数组/对象值的倍数过滤对象数组
- 在表中显示数组对象
- 在数组对象中分组
- 将第二个或多个数组推送到数组对象 AngularJS 中
- 重新排列 JavaScript 数组/对象
- 键上的javascript数组对象过滤器
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 用于跟踪购物车可用性的Javascript数组/对象/哈希表
- 重建有角度的java脚本数组对象
- 对JSON数组对象进行排序
- 连接与数组对象相关的文本:方式和位置
- 哪些浏览器和版本支持将常见的类数组对象直接传递到fn.apply()
- 如何使用Handlebars循环数组对象和模板
- 是否可以引用JS数组/对象中的另一个元素
- Javascript中的名称索引-数组/对象
- 数组长度不等于数组对象