1

I am new to angular and am having issues setting up a custom directive. The point of the custom directive is that when it is hovered over a button should be displayed that when clicked executes a function within the directive. The button is displaying fine but when I click it nothing happens and there are no javascript errors. I've included the directive code below as well as the html where I use it.

addStyleHoverModule.directive('addStyleHover', () => {
    return {
    scope: {
        selectedCampaign: "="
    },
    restrict: 'E',
    link: ($scope, $element, $attr) => {
        let template = `
            <div class="style-hover-box">
                <button class="btn btn-cc buy-it-now-btn add-to-cart-btn add-to-cart-btn-modal"
                    ng-click=addNewStyleCampaign({{selectedCampaign}})>Add to cart</button>
            </div>
        `;

        let templateElement;

        $element.on('mouseenter', () => {
            let {top, left} = $element[0].getBoundingClientRect();

            let t = $element.append(template);
            templateElement = t.find('div');
            templateElement.css({top: `${top}px`, left: `${left}px`});
        });

        $element.on('mouseleave', () => {
            if (templateElement) {
                templateElement.remove();
            }
        });

        $scope.$on('$destroy', () => {
            $element.off('mouseenter').off('mouseleave');
        });
    },
    controller: ($scope, $modal) => {
        $scope.addNewStyleCampaign = (campaign) => {
            debugger;
            let scope = $scope.$new();

            scope.campaignKey = campaign.key;

            $modal({
                templateUrl: addNewStyleModalTpl.name,
                controller: "AddNewStyleModalCtrl",
                scope: scope,
                onHide: () => {
                    $scope.loadCart();
                }
            });
        };
    }
};
});


export default addStyleHoverModule;

html:

<add-style-hover selected-campaign="similarCampaign">
<a ng-href="/{{similarCampaign.crossSellPath}}" ng-attr-title="{{similarCampaign.name}}">
<img class="img-responsive" style="width: 150px" ng-src="{{similarCampaign.mockupUrlSmall}}" ng-attr-alt="{{similarCampaign.name}}" ng-attr-nopin="{{similarCampaign.stealthy ? 'nopin' : ''}}"/>
</a>
</add-style-hover>

2 Answers 2

1

I believe you need to do it in quotes ng-click="addNewStyleCampaign(selectedCampaign)"

Sign up to request clarification or add additional context in comments.

Comments

0

Did you try using ng-click without the interpolation operator?

Try changing:

ng-click=addNewStyleCampaign({{selectedCampaign}})

to:

ng-click=addNewStyleCampaign(selectedCampaign)

within your directive template.

1 Comment

Yeah I tried that with the same result unfortunately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.