2017-01-31 45 views
0

我有兩個指令,父指令應該簡單地環繞其子。這個轉換被用於這個目的。 然而,任何其他指令,如ng-click,綁定到子指令元素的屬性不起作用(是不是編譯?)。在觸發指令內容上的角 - ng-click函數不會被觸發

這裏是JS:

(function(angular) { 
    'use strict'; 
angular.module('docsIsoFnBindExample', []) 
    .controller('Controller', ['$scope', '$timeout', function($scope, $timeout) { 
    $scope.name = 'Tobias'; 
    $scope.message = ''; 
    $scope.hideDialog = function(message) { 
     $scope.message = message; 
     $scope.dialogIsHidden = true; 
     $timeout(function() { 
     $scope.message = ''; 
     $scope.dialogIsHidden = false; 
     }, 2000); 
    }; 
    }]) //controller is not important now 

    .directive('myDialog', function() { //parent directive 
    return { 
     restrict: 'E', 
     transclude: true, 
     scope: { 
     'close': '&onClose' 
     }, 
     template: '<div class="alert"><a href class="close" ng-click="close({message: \'closing for now\'})">&times;</a><div ng-transclude></div></div>' 
    }; 
    }) 

    .directive('daka', function() { //child directive 
    return { 
     restrict: 'E', 
     scope: { 
     'input': '@' 
     }, 
     link: function(scope, element, attributes) { 
     scope.func= function() { 
      console.log("blablabla"); //no console output after click event 
     }; 
     } 
    }; 
    }); 
})(window.angular); 

HTML:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Example - example-directive-transclusion-scope-production</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> 
    <script src="script.js"></script> 
</head> 
<body ng-app="docsIsoFnBindExample"> 
    <div ng-controller="Controller"> 
    {{message}} 
    <my-dialog ng-hide="dialogIsHidden" on-close="hideDialog(message)"> 
    <daka ng-click="func()" input="11">BLABlABLA</daka> 
    </my-dialog> 
</div> 
</body> 
</html> 

回答

0

它試圖看看你的父母指令NG點擊。 因此,您可以爲您的子指令添加點擊事件。

.directive('daka', function() { //child directive 
return { 
    restrict: 'E', 
    scope: { 
    'input': '@' 
    }, 
    link: function(scope, element, attributes) { 
    element.on('click', function() { 
      alert('outcome clicked: '); 
     }); 
    } 
}; }); 

工作的jsfiddle鏈接 - https://jsfiddle.net/p2vht8sb/