使用Javascript中的GET方法检索项目并添加到AngularJS$scop中
我是AngularJs和Javascript新手。 我使用ng repeat在图库中显示图像,我在ng repeat中使用的“照片”如下所示:使用Javascript中的GET方法检索项目并添加到AngularJS$scop中,javascript,angularjs,Javascript,Angularjs,我是AngularJs和Javascript新手。 我使用ng repeat在图库中显示图像,我在ng repeat中使用的“照片”如下所示: $scope.photos = [ {src: 'http://farm9.staticflickr.com/8042/7918423710_e6dd168d7c_b.jpg', desc: 'Image 01'}, {src: 'http://farm9.staticflickr.com
$scope.photos = [
{src: 'http://farm9.staticflickr.com/8042/7918423710_e6dd168d7c_b.jpg', desc: 'Image 01'},
{src: 'http://farm9.staticflickr.com/8449/7918424278_4835c85e7a_b.jpg', desc: 'Image 02'},
{src: 'http://farm9.staticflickr.com/8457/7918424412_bb641455c7_b.jpg', desc: 'Image 03'},
{src: 'http://farm9.staticflickr.com/8179/7918424842_c79f7e345c_b.jpg', desc: 'Image 04'},
{src: 'http://farm9.staticflickr.com/8315/7918425138_b739f0df53_b.jpg', desc: 'Image 05'},
{src: 'http://farm9.staticflickr.com/8461/7918425364_fe6753aa75_b.jpg', desc: 'Image 06'}
];
我有一个GET方法,它给了我一个JSON:
[
{
"files": [
"https://storage.googleapis.com/acn-shelf-check-images/1.jpg",
"https://storage.googleapis.com/acn-shelf-check-images/2.jpg",
"https://storage.googleapis.com/acn-shelf-check-images/3.jpg",
"https://storage.googleapis.com/acn-shelf-check-images/4.jpg"
],
"id": 5710239819104256,
"name": "Shelf_1"
}
]
我想通过调用GET方法将更多图像添加到“photos”中,我可以使用以下代码获得图像列表:
$.getJSON("https://myappEngine.appspot.com/read/shelf/"+ 5710239819104256, function(result) {
$.each(result[0].files, function(i, file) {
});
});
angular.module('myApp', ['ngAnimate', 'ngTouch'])
.controller('MainCtrl', function ($scope, $http) {
// Set of Photos
$http.get("https://myApp.appspot.com/read/shelf/" +5710239819104256)
.then(function(response) {
console.log('response:', response);
var resData = response.data[0].files;
console.log('resData:', resData);
angular.forEach(resData, function(value) {
$scope.photos.push({
src: value,
desc: "Dummy description"
});
});
});
$scope.photos = [];
如何将GET方法的结果添加到“照片”列表中?试试:
$http.get("https://myappEngine.appspot.com/read/shelf/5710239819104256")
.then(function(response) {
console.log('response:', response);
$scope.imageUrls = response.result; // an array?
});
我已使用以下代码在范围列表中设置了从GET方法接收到的值:
$.getJSON("https://myappEngine.appspot.com/read/shelf/"+ 5710239819104256, function(result) {
$.each(result[0].files, function(i, file) {
});
});
angular.module('myApp', ['ngAnimate', 'ngTouch'])
.controller('MainCtrl', function ($scope, $http) {
// Set of Photos
$http.get("https://myApp.appspot.com/read/shelf/" +5710239819104256)
.then(function(response) {
console.log('response:', response);
var resData = response.data[0].files;
console.log('resData:', resData);
angular.forEach(resData, function(value) {
$scope.photos.push({
src: value,
desc: "Dummy description"
});
});
});
$scope.photos = [];
检查angular的
$http
对象,它可以执行GET请求并为您获取这些图像URL。请求解析为一个承诺,在该承诺中,您可以将映像路径分配给$scope变量。@TonyGW查看我的代码,您能帮我使用$http吗?问题是,我可以使用JS检索图像的url,但我不知道如何将它们添加到$scop@格罗本:我试过:$scope.photos.push(文件),但它不起作用!有什么帮助吗?但你为什么使用jquery?@Groben我对FE使用Angular、JS和jquery很陌生,所以我很困惑!!:(谢谢。你能告诉我如何将结果添加到$scope.photos中吗?正如我在帖子中解释的那样?你能给我一部分薪水吗?哈哈,stackoverflow根本不用于填鸭式喂食。