Chapter 1: Directive scope inheritance
by billy roberts
HTML
<script src="https://code.angularjs.org/1.3.2/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="MainCtrl">
<my-directive>
<p>HTML template</p>
<p>Scope from {{origin}}</p>
<p>Overwritten? {{overwrite}}</p>
</my-directive>
</div>
</div>
JavaScript
//directive inherits and overwrites parent controller data.
//the link f() receives the controller's scope in it's link f.
angular.module('myApp', [])
.controller('MainCtrl', function ($scope) {
$scope.overwrite = false;
$scope.origin = 'parent controller';
})
.directive('myDirective', function () {
return {
restrict: 'E',
link: function (scope) {
scope.overwrite = !!scope.origin;
scope.origin = 'link function';
}
};
});