2017-01-03 53 views
1

我是新來的角和我有問題設置自定義指令。自定義指令的要點是,當它被懸停在一個按鈕上時,應該顯示單擊時執行指令內的一個函數。該按鈕顯示正常,但當我點擊它沒有任何反應,並沒有JavaScript錯誤。我已經包含了下面的指令代碼以及我使用它的html。角度指令中的按鈕不執行ng-click函數

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> 

回答

0

你嘗試使用ng-click無插值算?

嘗試改變:

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

到:

ng-click=addNewStyleCampaign(selectedCampaign) 

您的指令模板內。

+0

是的,我試過了同樣的結果很遺憾 – thurmc

1

我相信你需要做的是在引號 NG點擊=「addNewStyleCampaign(所選廣告)」