HG2D - Template in Directive
Learn How to use template option in AngularJS Directive
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css">
<div style='padding:100px;' ng-controller="DemoCtrl">
<div whoiam>This text will be overwritten</div>
</div>
JavaScript
var App = angular.module('App', []);
App.run(function($rootScope) {
$rootScope.header = 'I am bound to rootsoap';
});
App.controller('DemoCtrl', function($scope) {
$scope.footer = 'I am bound to DemoCtrl';
});
App.directive('whoiam', function($http) {
return {
restrict: 'A',
template: '{{header}}<div class="thumbnail" style="width: 80px;"><div><img ng-src="{{data.owner.avatar_url}}"/></div><div class="caption"><p>{{data.name}}</p></div></div>{{footer}}',
link: function(scope, element, attrs) {
$http.get('https://api.github.com/repos/angular/angular.js').success(function(data) {
scope.data = data;
});
}
};
});