Idris Idris - 1 month ago 14
AngularJS Question

ng-view does not get the data from href?

I tried one product page using AngularJS. This the first time i am creating the page using AngularJS.

ng-view
is not working when i click the add to cart page. It does not show any respond.

I am verified the code with all the tutorials. It seems correct.

Can anyone please tell what i did wrong.

Below i mentioned the code. Sorry, cant able to put the code in jsfiddle. It does not bind the normal date.

Code:



var app = angular.module("myApp", ['ngRoute']);

app.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/red', {
templateUrl: 'dashboard.html',
controller: 'myCtrl',
})
.when('/green', {
templateUrl: 'custom_directives.html',
controller: 'myCtrl'
})
.when('/yellow', {
templateUrl: 'custom_directives.html',
controller: 'myCtrl'
})
.when('/black', {
templateUrl: 'custom_directives.html',
controller: 'myCtrl'
})
.when('/grey', {
templateUrl: 'custom_directives.html',
controller: 'myCtrl'
})
.when('/blue', {
templateUrl: 'custom_directives.html',
controller: 'myCtrl'
});

// configure html5 to get links working on jsfiddle
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
});
app.controller("myCtrl", function($scope) {
$scope.records = [{
"Name": "Alfreds Futterkiste",
"Country": "Germany",
"Dollar": "$21.00",
"URL": "red"
}, {
"Name": "Berglunds snabbköp",
"Country": "Sweden",
"Dollar": "$21.00",
"URL": "green"
}, {
"Name": "Centro comercial Moctezuma",
"Country": "Mexico",
"Dollar": "$21.00",
"URL": "yellow"
}, {
"Name": "Ernst Handel",
"Country": "Austria",
"Dollar": "$21.00",
"URL": "black"

}, {
"Name": "Ernst Jubilie",
"Country": "Canada",
"Dollar": "$21.00",
"URL": "grey"
}, {
"Name": "Idris_09090",
"Country": "India",
"Dollar": "$43.00",
"URL": "blue"
}]

});

li.list-group-item {
position: relative;
display: inline-block;
margin-bottom: -1px;
background-color: #D38585;
border: 1px solid #ddd;
width: 25%;
height: 210px;
}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>

<body ng-app="myApp" style="background-color: #2a2326;" ng-controller="myCtrl">

<div class="container">
<div class="well well-sm" style="background-color:#2a2326; color:white; text-align:center; font-size:30px;">
<h4><strong>Water Products</strong></h4>
</div>
<div class="item col-xs-12 col-lg-4" ng-repeat="x in records">
<div class="thumbnail">
<img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" />
<div class="caption">
<h4 class="group inner list-group-item-heading">
{{x.Name}}</h4>
<p class="group inner list-group-item-text">
{{x.Country}}</p>
<div class="row">
<div class="col-xs-12 col-md-6">
<p class="lead">
{{x.Dollar}}</p>
</div>
<div class="col-xs-12 col-md-6">
<a class="btn btn-success" ng-href="#{{x.URL}}">Add to cart</a>
</div>
</div>
</div>
</div>
</div>
<div ng-view></div>
</div>





Please help on this.

Answer

remove

$locationProvider.html5Mode({
       enabled: true,
       requireBase: false
 });

change your url to <a class="btn btn-success" ng-href="#/{{x.URL}}">Add to cart</a>

demo:

https://plnkr.co/edit/5odGEUq0eK8tGR8IxohW?p=preview