I am developing a website in AngularJS-1.6.4 for product advertisement and It is the multi-page website. This website has two webpage index.html and product.html. The functionality of this project is - On load of index.html, product list will be fetched from the database and show this product list in index.html. Each product Will be the link and on click on this product, a request will go to the server for getting this product details and show this product details in product.html page. index.html is - |
// create the module and name it
var gelovenIndia = angular.module('apparel', ['ui.router']);
app.config(['$httpProvider', '$stateProvider', '$urlRouterProvider', function($httpProvider, $stateProvider, $urlRouterProvider) {
$stateProvider
.state('product',{
url:"/product",
templateUrl: 'pages/product.html',
controller: 'productController'
});
}]);
/*
// configure our routes
gelovenIndia.config(function ($routeProvider,$locationProvider) {
console.log("1");
$locationProvider.hashPrefix('');
$routeProvider
// route for the Product page
.when('/product', {
templateUrl : 'pages/product.html',
controller : 'productController'
});
}); */
gelovenIndia.service('productService', function() {
console.log("2");
var product;
var addProduct = function(newObj) {
console.log("3");
console.log("adding product to service", newObj);
product = newObj;
};
var getProduct = function(){
console.log("4");
console.log("getting product from service", product);
return product;
};
return {
addProduct: addProduct,
getProduct: getProduct
};
});
gelovenIndia.controller('productController', function($scope, $http, $location,$window, productService) {
console.log("In productController");
$scope.product = {};
$scope.productById = productService.getProduct();
console.log('Product in productController', $scope.productById);
$scope.onloadFun = function() {
alert(1);
console.log("In Onload Function");
$http({
method : 'GET',
url : 'productData',
data : angular.toJson($scope.userform),
headers : {
'Content-Type' : 'application/json'
}
}).then(function(response) {
alert("Got response for Product Data");
console.log("Got response for Product Data");
console.log(response.data);
$scope.products = response.data;
});
};
$scope.getProductById = function(URL) {
alert("Product URL is :"+URL);
console.log('In getProductById');
$scope.product.product_Url = URL;
console.log($scope.product);
$http({
method : 'POST',
url : 'productData',
data : angular.toJson($scope.product),
headers : {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
alert("Got response for Product");
console.log("response in getProductById", response.data);
$scope.productById = response.data;
productService.addProduct(response.data);
$location.path('/product');
//$window.location.href = '/product';
console.log("10");
});
};
});
<body ng-controller="productController">
<div style="margin-top: 107px;">
<img src="{{product.product_Url}}" style="width:150px">
</div>
</body>