Created
July 9, 2014 12:47
-
-
Save sebald/bdf8a4f194e1504375c5 to your computer and use it in GitHub Desktop.
Testing Angular directive replaceWith
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsbin.com/gowup/2/edit