AngularJS Directive Scope Property Inheritance

by ksevksev

HTML

<div ng-app="myApp">
    <div ng-controller="myCtrl">
        <div my-directive>{{test.name}}</div>
    </div>
</div>

CSS

</style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<style>
.parent { background-color:tan; }
​.ng-invalid { border: 1px solid red; }​

JavaScript

'use strict';

var myApp = angular.module('myApp', []);

myApp.controller('myCtrl', function ($scope) {
    $scope.test = {name:"name", value:"value"};
});

myApp.directive("myDirective", function () {
    return {
        replace: true,
        restrict: 'A',
        scope: {test: '='},
        template: '<div class="parent"><div>This is the parent Div.</div><div>Value={{test}}</div></div>',
        link: function (scope, element, attrs) {
            console.log("scope.test=["+scope.test +"]");
            console.log("scope.$parent.test=["+scope.$parent.test.name+"]");
        }
    };
});