2016-09-17 126 views
0

處理事件我有以下AngularJS指令(在底部)時,如果點擊鏈接saveClick()然後應顯示的警告窗口:AngularJS從指令的控制器

<span save-click="alert('hallo ich binssdsd')" data-ng-dropdown-multiselect options="vm.translatedRoles" selected-model="vm.selectedRoles" external-id-prop="label"></span> 

但沒有任何反應。有誰知道我做錯了什麼?

directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$compile', '$parse', '$rootScope', 
function ($filter, $document, $compile, $parse, $rootScope) { 

    return { 
     restrict: 'AE', 
     scope: { 
      selectedModel: '=', 
      options: '=', 
      extraSettings: '=', 
      events: '=', 
      searchFilter: '=?', 
      translationTexts: '=', 
      groupBy: '@', 
      saveClick: '&' 
     }, 
     template: function (element, attrs) { 
      var checkboxes = attrs.checkboxes ? true : false; 
      var groups = attrs.groupBy ? true : false; 

      var template = '<div class="multiselect-parent btn-group dropdown-multiselect">'; 
      template += '<li><a data-ng-click="saveClick()"><span class="glyphicon glyphicon-floppy-disk"></span> {{texts.save}}</a>'; 
      ... 
+0

所以你要調用一個'控制器'f從'指令'的聯合?看看這[小提琴](http://jsfiddle.net/u748hLvn/4/)。通過[如何創建一個最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) – Paritosh

回答

0

您需要將鏈接函數添加到指令定義對象以使警報生效。

下面是示例代碼:

directive.js

var app = angular.module("myApp", []) 

app.directive('testAlert',function() { 
    return { 
    restrict: 'E', 
    template: '<button ng-click="alertClick()">Test</button>', 
    link: function (scope) { 
     scope.alertClick = function() { 
     alert('Welcome!'); 
    }; 
    } 
    } 
}); 

的index.html

<div ng-app="myApp"> 
    <test-alert></test-alert> 
</div> 

WorkingExample

相關問題