I'm attempting to bind an event handler to elements that don't yet exist within the DOM but am running into issues. I think where the problem begins is at line #51, where I attempt to find an element that doesn't exist (because ng-repeat hasn't yet generated it). Is there a way around this without creating a binding that is too general (e.g., $("a").on("click", function () {});)?
cover-photo-carousel.js
1 var MyApp = angular.module("MyApp");
2
3 MyApp.directive("coverPhotoCarousel", ["$compile", function ($compile) {
4 return {
5 restrict: "EA",
6 replace: true,
7 templateUrl: "cover-photo-carousel.html",
8 scope: true,
9 controller: function ($scope) {
10 $scope.images = [
11 {
12 title: "",
13 url: "http://blah.com/abc.jpg",
14 description: "",
15 cover: true
16 },
17 {
18 title: "",
19 url: "http://blah.com/abc.jpg",
20 description: ""
21 },
22 {
23 title: "",
24 url: "http://blah.com/abc.jpg",
25 description: ""
26 },
27 {
28 title: "",
29 url: "http://blah.com/abc.jpg",
30 description: ""
31 }
32 ];
33 $scope.bark = function () {
34 console.log("Something.");
35 };
36 },
37 link: function (scope, element, attributes) {
38
39 if (scope.images.length > 1) {
40 element.find(".cover-photo-control .glyphicon").show();
41 }
42
43 scope.images.forEach(function (value, index) {
44 if (value.cover) {
45 element.css({
46 "background-image": "url(" + value.url + ")"
47 });
48 }
49 });
50
51 element.find(".thumbnails-container .thumbnails ul li a").on("click", function (e) {
52 e.preventDefault();
53 element.css({
54 "background-image": "url(" + scope.images[1].url + ")"
55 });
56 });
57 }
58 }
59 }]);
cover-photo-carousel.html
<div class="cover-photo-carousel">
<div class="cover-photo-control back text-center">
<span class="glyphicon glyphicon-chevron-left"></span>
</div>
<div class="cover-photo-control forward text-center">
<span class="glyphicon glyphicon-chevron-right"></span>
</div>
<div class="thumbnails-container">
<div class="thumbnails">
<ul class="clearfix">
<li ng-repeat="image in images">
<a href="#"><img src="{{image.url}}" alt="" width="90" /></a>
</li>
</ul>
</div>
</div>
</div>