1

我有以下 angularjs 指令,我希望能够使用 ng-click 在 property-slider.html 中执行 showMap()。我错过了什么?

(function() {
    'use strict';

    angular
        .module('myapp')
        .directive('propertySlider', propertySlider);

    function propertySlider($timeout) {

        return {
            restrict: 'E',
            templateUrl: 'property-slider.html',
            replace: true,
            scope: {
                property: '=',
                photos: '='
            },
            link: function(scope, element) {
                $timeout(function(){

                    var slider = element.flickity({
                        cellAlign: 'left',
                        cellSelector: '.gallery-cell',
                        lazyLoad: true,
                        wrapAround: true,
                        initialIndex: 1
                    });

                    var showMap = function(){
                        slider.flickity('select', 0);
                    };

                },500);

            }
        };

    }

})();
4

2 回答 2

1

两个问题....功能需要分配给范围,而您不需要在内部创建它$timeout

link: function(scope, element) {

     scope.showMap = function () {
         element.flickity('select', 0);
     };

     $timeout(function () {

          element.flickity({
             cellAlign: 'left',
             cellSelector: '.gallery-cell',
             lazyLoad: true,
             wrapAround: true,
             initialIndex: 1
         });

     }, 500);
}
于 2015-09-26T00:10:44.100 回答
1

除了使用ng-click您还可以将您的方法“私有”到您的指令并检测元素上的事件:

link: function(scope, element, attrs) {
    element.on('click', function(e) {
        showMap();
    });

    var showMap = ...
}
于 2015-09-26T00:15:42.620 回答