trigger click event from angularjs directive

后端 未结 4 2116
清歌不尽
清歌不尽 2021-02-02 10:05

How can i trigger a click event for li elements specifying their index from the angularjs directive? I have tried using $first for triggering click for the first element, but it

4条回答
  •  忘了有多久
    2021-02-02 10:33

    This is more the Angular way to do it: http://plnkr.co/edit/xYNX47EsYvl4aRuGZmvo?p=preview

    1. I added $scope.selectedItem that gets you past your first problem (defaulting the image)
    2. I added $scope.setSelectedItem and called it in ng-click. Your final requirements may be different, but using a directive to bind click and change src was overkill, since most of it can be handled with template
    3. Notice use of ngSrc to avoid errant server calls on initial load
    4. You'll need to adjust some styles to get the image positioned right in the div. If you really need to use background-image, then you'll need a directive like ngSrc that defers setting the background-image style until after real data has loaded.

提交回复
热议问题