事件 – 从angularjs指令触发点击事件

前端之家收集整理的这篇文章主要介绍了事件 – 从angularjs指令触发点击事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我如何触发一个点击事件的li元素指定它们的indexjs指令的索引?
我尝试使用$ first触发第一个元素的点击,但它不工作。

感谢任何帮助。

这可能是一种不同的方式来实现这一点。传入索引和项目的指令,并让指令在模板中设置html:

演示:http://plnkr.co/edit/ybcNosdPA76J1IqXjcGG?p=preview

html:

<ul id="thumbnails">
    <li class="thumbnail" ng-repeat="item in items" options='#my-container' itemdata='item' index="$index">

    </li>
  </ul>

js指令:

app.directive('thumbnail',[function() {
  return {
    restrict: 'CA',replace: false,transclude: false,scope: {
        index: '=index',item: '=itemdata'
    },template: '<a href="#"><img src="{{item.src}}" alt="{{item.alt}}" /></a>',link: function(scope,elem,attrs) {
        if (parseInt(scope.index) == 0) {
            angular.element(attrs.options).css({'background-image':'url('+ scope.item.src +')'});
        }

        elem.bind('click',function() {
            var src = elem.find('img').attr('src');

            // call your SmoothZoom here
            angular.element(attrs.options).css({'background-image':'url('+ scope.item.src +')'});
        });
    }
}
}]);

在另一个答案中指出,您可能会更好地添加点击图像。

更新

演示的链接不正确。它已更新为:http://plnkr.co/edit/ybcNosdPA76J1IqXjcGG?p=preview

原文链接:https://www.f2er.com/angularjs/145042.html

猜你在找的Angularjs相关文章