我正在尝试在使用ng-repeat的div中插入href链接。此href链接需要与使用ng-repeat创建的每个div不同,其值是AngularJS $范围变量的一部分。
这是我的JS代码:
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));
});
尝试HTML代码:
<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>
这段代码不让我点击任何地方,我的光标就变成了一个指针,就是这样。
HTML代码的第二次尝试:
<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>
使用此代码,我可以单击,但它会将我带到file:///Users/Marc/firebase/trip.html,因此它缺少参数userid和tripid(userid是AngularJS之外的变量,而tripid是AngularJS $ scope.repeatData)下的data.tripKey
答案 0 :(得分:1)
看起来你过于复杂了。没有理由使用我能看到的onclick。我将其更改为:
<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>
除非你真的想要复制每个锚内的所有图像,否则我也不认为你需要第二次重复。