没有模板的Angular指令 - 从指令中指定ng-click函数

时间:2016-03-24 23:32:23

标签: angularjs angularjs-directive isolate-scope

如何在应用了我的指令属性的元素上设置ng-click功能?在我的指令模板出现问题之后,因为它跨越多个tr,我导致使用ng-repeat-start / end,即没有模板。所以,我的问题是:将指令元素的内容放入指令的隔离范围的正确方法是什么,所以我可以为它分配值/函数,就好像它是模板一样?

//控制器范围

Pusher.log = function(message) {
  if (window.console && window.console.log) {
    window.console.log(message);
  }
};

var pusher =
  new Pusher('3h23kjh322kknkjsad', {
              authEndpoint : 'http://localhost:3000/pusher/auth',
              auth: {
                headers : { 'Authorization':'bb692c03c2d8402ca3a5' }
              }
             });

var channel = pusher.subscribe('private-live_admin');
channel.bind('update', function(data) {
  alert(data);
});

//指令

<tr campaign-item ng-repeat-start="campaign in vm.allCampaigns.results | orderBy:vm.params.sort:vm.sortReverse track by $index" ng-if="vm.allCampaigns.totalCount > 0" campaign="campaign" close-preview="vm.hideCampaignPreview()" class="campaign-tr" id="campaign-{{campaign.id}}" ng-class="{'selected': showCampaignDetails}" user-role="{{vm.user.role}}" campaign-options="" campaign-detail="" show-campaign-details="">
  <td>{{ campaign.name }}</td>
  <td>{{ campaign.priority }}</td>
  <td>{{ campaign.status }}</td>
  <td>{{ campaign.createdBy }}</td>
  <td>{{ campaign.approvedBy }}</td>
  <td ng-show="item.releaseDate">{{ campaign.releaseDate * 1000 | date:'short' || ''}}</td>
  <td ng-show="!item.releaseDate"></td>
  <td ng-show="item.expirationDate">{{ campaign.expirationDate * 1000 | date:'short' }}</td>
  <td ng-show="!item.expirationDate"></td>
  <td>
    <select id="campaign-options" name="campaignOptions" class="form-control" ng-model="selectedCampaignOption" ng-options="option for option in campaignOptions track by $index">
    </select>
  </td>
</tr>
<tr class="campaign-description campaign-{{campaign.id}}" ng-show="showCampaignDetails" ng-class="{'selected': showCampaignDetails}">
  <td colspan="8">
    <p> {{ campaignDetails.description }}</p>
  </td>
</tr>

2 个答案:

答案 0 :(得分:1)

您可以从指令中添加ImportError: No module named 'pygame.locals'; 'pygame' is not a package,如下所示:(如果您提供小提琴或plnkr对我们来说更容易测试)

ng-click

如果你在控制器上有onClick()函数,则添加controllerName.onClick()

 element.attr("ng-click", "onClick()");

然后在最后

element.attr("ng-click", "vm.onClick()");

$compile(element.contents())(scope);

答案 1 :(得分:0)

另一种选择是

element.bind('click', function(e) {
  // Handle onclick event here.
});