Skip to content

Instantly share code, notes, and snippets.

@sebald
Created July 9, 2014 12:47
Show Gist options
  • Save sebald/bdf8a4f194e1504375c5 to your computer and use it in GitHub Desktop.
Save sebald/bdf8a4f194e1504375c5 to your computer and use it in GitHub Desktop.
Testing Angular directive replaceWith
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-app="mine">
<div ng-controller="myCtrl as ctrl">
<my class="red" ng-click="increment()"></my>
<pre><code>{{ count }}</code></pre>
<button ng-click="increment()">++</button>
</div>
</body>
</html>
angular.module('mine', [])
.controller('myCtrl', function ( $scope ) {
$scope.count = 0;
$scope.increment = function () {
$scope.count = $scope.count + 1 ;
};
})
.directive('my', function ( $compile ) {
return {
restrict: 'E',
compile: function ( element, attrs ) {
var template = '<div' +
' ng-click="' + attrs.ngClick + '"'+
'>WTF!</div>';
console.log( attrs.ngClick );
element.replaceWith(template);
}
};
});
@sebald
Copy link
Author

sebald commented Jul 9, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment