var app = angular.module('plunker', []);
app.directive('myDirective', function($compile) {
return {
compile: function(elm) {
var html = elm.html();
return function(_scope, _element, _attrs){
var target = $('#'+_attrs.myDirective);
target.html(html);
$compile(target.contents())(_scope);
_element.empty();
}
}
};
});
app.controller('MainCtrl', function($scope) {
$scope.items = ['A','B','C','D'];
$scope.doSomething = function(){
alert('Scope works');
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script data-require="angular.js@1.3.0" data-semver="1.3.0" src="//code.angularjs.org/1.3.0/angular.js"></script>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
</head>
<body>
<div id="foo">
Target Div
</div>
<div ng-controller="MainCtrl">
View Controller
<div my-directive="foo">
<button ng-click="doSomething()">Click Me</button>
<ul>
<li ng-repeat="item in items">
{{item}}
</li>
</ul>
</div>
</div>
</body>
</html>
/* Put your css in here */