I'm trying to build multiple divs from an ajax response. The following is the module that I'm using.
angular.module('appBuyItemModule', [])
.controller('buyItemController', ['$scope', '$http', function ($scope, $http) {
$scope.init = function () {
$http({
method: 'GET',
url: '/get-items',
contentType: 'application/json',
}).success(function (response) {
for (let index = 0; index < response.length; index++) {
console.log(response[index]["item_name"]);
console.log(response[index]["item_brand"]);
console.log(response[index]["item_picture"]);
console.log(response[index]["item_description"]);
console.log(response[index]["item_quantity"]);
//Build the divs...
}
console.log('Success!!');
}).error(function () {
console.log('Error!!');
});
};
}]);
The divs should be built inside the for loop and they must allow for onClick events. I have added it using static html and the events were triggered successfully but I want to replace the following html code with angularJS:
<div class="col-md-3 col-sm-6 hero-feature">
<div class="thumbnail">
<img src="https://dsw.scene7.com/is/image/DSWShoes/377442_007_ss_01?$pdp-image$" alt="">
<div class="caption">
<h3>2-Feature Label</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p>
<button name="pink_shoes" ng-controller="buyItemController" class="btn btn-primary"
ng-click="info.item_number = 'item_2'; buy()">Buy Now
</button>
<button ng-controller="shopController" class="btn btn-default" ng-click="confirm()">More info
</button>
</p>
</div>
</div>
</div>
For example: in "ng-click="info.item_number = 'item_2'; buy()" I want to replace 'item_2' with 'response[index]["item_name"]', the picture link with my json response etc.Can anyone please point me to the right direction ? I'm relatively new to angularJS.
I have managed to make it work partially in JavaScript but it looks really bad and it doesn't work. Please see below:
let itemsContainer = document.getElementById('items-container');
let ele = document.createElement('div');
itemsContainer.appendChild(ele);
ele.innerHTML = "<div class=\"col-md-3 col-sm-6 hero-feature\">\n" +
"<div class=\"thumbnail\">\n" +
"<img src=\"https://dsw.scene7.com/is/image/DSWShoes/377442_007_ss_01?$pdp-image$\" alt=\"\">\n" +
"<div class=\"caption\">\n" +
"<h3>2-Feature Label</h3>\n" +
"<p>response[index][\"item_description\"]</p>\n" +
"<p>\n" +
"<button name=\"response[index][\"item_brand\"]\" ng-controller=\"buyItemController\" class=\"btn btn-primary\"\n" +
" ng-click=\"info.item_number=\"response[index][\"item_name\"]\";buy()\">Buy Now\n" +
"</button>\n" +
"<button ng-controller=\"shopController\" class=\"btn btn-default\" ng-click=\"confirm()\">More info\n" +
"</button>\n" +
"</p>\n" +
"</div>\n" +
"</div>\n" +
"</div>";