Reputation: 629
I'm trying to insert a href link inside a div that uses ng-repeat. This href link needs to be different from each div created with ng-repeat, its value being part of AngularJS $scope variable.
Here is my JS code:
var app = angular.module('test', []);
app.controller('MainCtrl', function($scope) {
var database = firebase.database();
database.ref(`trips/${userid}/trips`).once('value')
// will return you all the photo objects inside the `tripId`
.then(photosSnap => {
var trips = [];
photosSnap.forEach((trip) => {
trips.push({
tripKey: trip.key,
tripName: trip.val().name,
tripPhotoUrl: trip.val().photourl,
tripBeginDate: ConvertDate(trip.val().begindate),
tripEndDate: ConvertDate(trip.val().enddate)
});
});
$scope.repeatData = trips;
// the array is placed into the scope of angular controller, later used with ng-repeat
// $scope.data = repeatData;
// $scope.value = repeatData;
// apply immediatly, otherwise doesn't see the changes
$scope.$apply();
// returns the array in the console to check values
console.log($scope);
}).catch(err => alert(err));
});
One try of HTML code:
<div class="main" ng-app="test" ng-controller="MainCtrl">
<div id="usertripinfo" ng-repeat="data in repeatData" style="background-image: url({{data.tripPhotoUrl}}); height: 300px; width: 600px; cursor: pointer;" href="trip.html" onclick="location.href=this.href+'?userid='+userid+'&tripid='+data.tripKey;return false;">
{{data.tripName}}
{{data.tripKey}}
{{data.tripBeginDate}}
{{data.tripEndDate}}
</div>
</div>
This code just doesn't let me click anywhere, my cursor just becomes a pointer and that's it.
A second try of HTML code:
<div class="main" ng-app="test" ng-controller="MainCtrl">
<a ng-repeat="data in repeatData" href="trip.html" onclick="location.href=this.href+'?userid='+userid+'&tripid='+data.tripKey;return false;">
<div id="usertripinfo" ng-repeat="data in repeatData" style="background-image: url({{data.tripPhotoUrl}}); height: 300px; width: 600px; cursor: pointer;">
{{data.tripName}}
{{data.tripKey}}
{{data.tripBeginDate}}
{{data.tripEndDate}}
</div>
</a>
</div>
With this code, I can click, but it brings me to file:///Users/Marc/firebase/trip.html, so it's lacking the parameters userid and tripid (userid being a variable outside AngularJS, and tripid being data.tripKey under AngularJS $scope.repeatData)
Upvotes: 0
Views: 2491
Reputation: 5176
It looks like you're overcomplicating this to me. There's no reason for using onclick that I can see. I'd change it to something like:
<div class="main" ng-app="test" ng-controller="MainCtrl">
<a ng-repeat="data in repeatData" href="trip.html?userid={{userid}}&tripid={{data.tripKey}}">
<div id="usertripinfo" style="background-image: url({{data.tripPhotoUrl}}); height: 300px; width: 600px; cursor: pointer;">
{{data.tripName}}
{{data.tripKey}}
{{data.tripBeginDate}}
{{data.tripEndDate}}
</div>
</a>
</div>
I also don't think you need the 2nd ng-repeat unless you really want to duplicate all of the images inside of each anchor.
Upvotes: 1