我正在修改Altair Material Design Template(http://themeforest.net/item/altair-admin-material-design-premium-template/12190654)&有以下Angular JS控制器。
我正在成功调用API &在for循环中创建变量。然后我想将它们写入$ scope.dragulaItems。这似乎在控制台日志中工作正常,但是该数组在页面上显示不正确。
的HTML DIV如下:
<div class="uk-grid uk-grid-width-small-1-2 uk-grid-width-medium-1-3 uk-grid-width-large-1-5" data-uk-grid-margin dragula='"first-dragula"' dragula-model='dragulaItems'>
<div ng-repeat="item in dragulaItems">
我看到了正确数量的页面(20周风格的div)的响应返回,但在其中没有内容。
angular
.module('altairApp',[angularDragula(angular)])
.controller('addResultsCtrl', [
'$scope',
'$rootScope',
'utils',
function ($scope,$rootScope,utils) {
// Search
SessionKey = "YXZmLwmNiT3vTuXwbbQrSY8ibrGhLuWLZag3DCCH2zu7w9dO3b";
$("#addSearch").submit(function(event) {
event.preventDefault();
var searchTerm = $("#addSearch-term").val();
var settings = {
"async": true,
"crossDomain": true,
"url": "https://secure.techfor.com/labsapi/restservice.aspx?command=PRODUCTSEARCH&searchtext="+searchTerm+"&extendedinfo=n&page=1&sessionkey="+SessionKey+"",
"method": "GET",
"headers": {
"cache-control": "no-cache",
}
}
$.ajax(settings).done(function (response) {
//console.log(response);
$("#searchTerm").html(searchTerm);
$scope.dragulaItems = [];
for (i in response.Products) {
var name = response.Products[i].Name;
var imagePath = response.Products[i].ImagePath;
var EAN = response.Products[i].EANBarcode;
var price = response.Products[i].Price;
var offer = response.Products[i].OfferPromotion;
var offerValid = response.Products[i].OfferValidity;
$scope.dragulaItems.push ("{\"Name\": \""+name+"\",\"Price\": \""+price+"\",\"EANBarcode\": \""+EAN+"\",\"ImagePath\": \""+imagePath+"\",\"OfferPromotion\": \""+offer+"\",\"OfferValidity\": \""+offerValid+"\"}");
}
console.log($scope.dragulaItems);
});
});
}
]);
如果我手动更改$ scope.dragulaItems = [];要说:
$scope.dragulaItems = [
{"Name":"Bob"},
{"Name":"Reggie"}
];
我得到2个项目返回正确显示名称字段。我很困惑,因为阵列中有20个项目正在通过,但其中的内容不是。
不要使用'对... in'通过阵列迭代! – undefined