I have the following problem. I have a custom directive with some content. There is a spacial place inside this directive that should be dynamic (some custom functionality).
The directive looks something like this:
var app = angular.module('plunker', []);
app.directive('mydirective', mydirective);
function mydirective() {
return {
restrict : "E",
scope : {
customContent: '@'
},
template : "<div>Some common directive stuff in here! <div id='customContent'></div></div>",
compile: function(element, attr) {
return {
post: function($scope, element, attr) {
console.log("POST");
},
pre: function($scope, element, attr) {
console.log("PRE: " + $scope.customContent);
if($scope.customContent) {
var customContent = (angular.element(document.getElementById('customContent')));
customContent.append($scope.customContent);
}
}
};
}
}
}
As you can see there is a div tag with id='customContent'
Now whenever I use this directive from a controller, I would like to be able not only to inject special custom content inside this directive, but also to provide the scoping functionality.
The required result should be:
controller
app.controller('MainCtrl', function($scope) {
$scope.func = function() { // THIS DOESN'T WORK
alert(1);
}
});
html
<mydirective custom-content="<button ng-controller='MainCtrl' ng-click='func()'>Test</button>"></mydirective>
As you can see I am injecting the passed custom-content as parameter in the PRE linking function. Unfortunately I was not able to hook the func() function into the controller's scope, because that's the place that I would like to be able to control the things from.
Thank you in advance for any help!
This is the plunker: https://plnkr.co/edit/I4WAQ20ugeDiackxXqUz?p=preview