Angular directive to replace itself

by Mark Coleman

HTML

<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="TestCtrl" ng-app="myApp">
    <row items="items">***</row>
</div>

CSS

row {
    color: red;
}

JavaScript

angular.module('myApp', [])
    .controller("TestCtrl", function ($scope) {
    $scope.items = [1, 2, 3, 4];
})
.directive('row', function ($compile) {
    return {
        restrict: 'E',
        scope: {
            items: "="
        },
        link: function (scope, element, attrs) {
            var html ='<div ng-repeat="item in items">I should not be red</div>';
            var e =$compile(html)(scope);
            element.replaceWith(e);
        }
    };
});