【问题标题】:Use href in an AngularJS ng-repeat div在 AngularJS ng-repeat div 中使用 href
【发布时间】: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


【解决方案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>

我也不认为你需要第二次 ng-repeat,除非你真的想复制每个锚点内的所有图像。

【讨论】:

  • 我使用了你的代码,现在我可以点击每个 div,虽然它把我带到了这个链接:“file:///Users/Marc/firebase/trip.html?userid=&tripid= " 好像不能写URL中的参数
  • tripKey 中的 K 没有大写。这可能就是那里的问题。控制器无法评估 userId。我建议在构造函数中将 $scope.userid = userid 添加到您的控制器代码中,因此请确保它在那里可用。为清楚起见,我将在答案中编辑tripKey。
  • 我将 $scope.userid = userid 添加到了我的控制器中,它起作用了,谢谢!
  • 还有一个问题,我正在尝试在我的单页应用程序中添加另一个控制器。似乎我的第二个控制器没有被考虑在内。这是我的代码的 jsfiddle:jsfiddle.net/5jv44y0t/1
  • 看起来不错。您可能应该为此提出一个新问题。确保您说明了哪些症状使第二个控制器看起来不可用。另外,我希望你的 javascript 真的在外部文件中。 :)
猜你喜欢
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多