AngularJS Tutorial - Directive to replace itself








The following code shows how to use Directive to replace itself.

Example


<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<style type='text/css'>
    row {<!--from   w ww .j a  v a  2s  . co  m-->
        color: red;
    }
</style>
<script type='text/javascript'>

angular.module('myApp',[])
.directive('row', function($compile) {
    return {
        restrict: 'E',
        link: function(scope, element, attrs) {
            element.html('<div>I should not be red</div>');
            $compile(element.contents())(scope);
        }
    }
});
</script>
</head>
<body ng-app="myApp">
  <row>***</row>
</body>
</html>

The code above is rendered as follows: