【发布时间】:2017-12-31 12:23:08
【问题描述】:
我正在尝试在使用 ng-repeat 的 div 中插入一个 href 链接。这个 href 链接需要与使用 ng-repeat 创建的每个 div 不同,它的值是 AngularJS $scope 变量的一部分。
这是我的 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 和 triid(userid 是 AngularJS 之外的变量,而 triid 是AngularJS下的data.tripKey $scope.repeatData)
【问题讨论】:
-
您需要指向外部页面或 AngularJs 应用程序中其他视图的链接?
-
我需要链接到外部页面
-
您是否通过
file://协议为您的网站提供服务,因为地址栏中的地址开头有file://?
标签: javascript angularjs firebase firebase-realtime-database angularjs-ng-repeat