【发布时间】:2017-12-06 09:44:06
【问题描述】:
我是 angularjs 的新手。我正在做一个项目,其中我有三个 html 文件 index.html 用于 ng-view,home.html 用于显示各种产品,这些产品来自数据库。我能够从数据库中获取产品并能够在 home.html 上显示这些产品。 home.html 中的每个产品都是 product.html 页面的链接。 Product.html 页面用于显示单个产品的详细信息。
我的问题是当我第一次点击 home.html 中的产品时,product.html 页面显示空白。在下一次点击 home.html 中的任何产品时,product.html 页面显示之前的点击数据。这种模式在每次点击时都会发生,只显示之前的点击数据。
home.html 是 -
<div ng-controller="productController" class="homeMainDiv">
<!-- Product DIV Start -->
<div data-ng-init="onloadFun()">
<div>
<!--
<div id="product" ng-repeat="product in products">
<table>
<tr>
<td>
<a target="_blank" href={{product.product_Url}}>
<img src="{{product.product_Url}}" style="width:150px">
</a>
</td>
</tr>
</table>
-->
<table style="width:100%">
<tr ng-repeat="product in products" ng-switch on="$index % 3">
<td ng-switch-when="0">
<a target="_blank" href={{products[$index].product_Url}}>
<img src="{{products[$index].product_Url}}" style="width:150px">
</a>
<a href="#product" style="cursor: pointer; margin-top: 5px;" ng-click="getProductById(products[$index].product_Url)">View Product Details and Buy Options</a>
</td>
<td ng-switch-when="0">
<span ng-show="products[$index+1]">
<a target="_blank" href={{products[$index+1].product_Url}}>
<img src="{{products[$index+1].product_Url}}" style="width:150px">
</a>
<a href="#product" style="cursor: pointer; margin-top: 5px;" ng-click="getProductById(products[$index+1].product_Url)">View Product Details and Buy Options</a>
</span>
</td>
<td ng-switch-when="0">
<span ng-show="products[$index+2]">
<a target="_blank" href={{products[$index+2].product_Url}}>
<img src="{{products[$index+2].product_Url}}" style="width:150px">
</a>
<a href="#product" style="cursor: pointer; margin-top: 5px;" ng-click="getProductById(products[$index+2].product_Url)">View Product Details and Buy Options</a>
</span>
</td>
</tr>
</table>
</div>
</div>
</div>
product.html 是 -
<div ng-controller="productController">
<div style="margin-top: 307px;">
<h1>Hello Product</h1>
<img src="{{productById.product_Url}}" style="width:150px">
<br>
<a href="#">View Product Details and Buy Options</a>
</div>
</div>
script.js 是 -
var gelovenIndia = angular.module('apparel', ['ngRoute']);
gelovenIndia.config(function ($routeProvider,$locationProvider) {
console.log("In config");
$locationProvider.hashPrefix('');
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/home.html',
controller : 'productController'
})
// route for the your story page
.when('/product', {
templateUrl : 'pages/product.html',
controller : 'productController'
});
});
gelovenIndia.controller('productController', function($scope, $http, $location, productService) {
console.log("In productController");
$scope.product = {};
//console.log("$scope.products", $scope.products);
$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:", response.data);
$scope.products = response.data;
console.log("onloadFun - $scope.products", $scope.products);
});
};
$scope.getProductById = function(URL) {
alert("Product URL is :"+URL);
//$scope.productById = productService.getProduct();
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");
});
};
});
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
};
});
请告诉我,有什么解决办法。
【问题讨论】:
-
使用 ng-href 代替 href 并将您的 ng-href 包含在 ""
-
@Vivz:我尝试使用 ng-href 但它没有用。更改后我的代码类似于 - 查看产品详情和购买选项
-
你使用的是什么版本的 angularjs?我认为href可能不正确。您可以尝试使用 $location.path 重定向到 product.html。你可以检查这个stackoverflow.com/questions/41211875/…
-
@vivz:我使用的是 angularjs 1.6.4。对于重定向,我只使用 $location.path。它类似于 $location.path('/product');
-
如果您使用 ng-click 和 $location 进行重定向,则此行不需要 href 查看产品详情和购买选项
标签: javascript java angularjs