AngularJS Inherited Data

AngularJS example to display inherited data

by andreas_karz

HTML

<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.0.4/css/bootstrap-combined.min.css">
<div ng-controller="mainController">
    {{users}}
</div>

JavaScript

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

myApp.controller('mainController', function($scope) {
    

    $scope.users = [
        {
            firstName : 'Hans', 
            lastName : 'Muster'
        },  
        {
            firstName : 'John', 
            lastName : 'Doe'
        },   
        {
            firstName : 'Heidi', 
            lastName : 'Demo'
        }  
    ];
    

});

myApp.directive('userinfo', function() {
    var directive = {};

    directive.restrict = 'E';

    directive.templateUrl = 'docwindow.html';
    
    directive.scope = {
        user : "=user"
    }

    directive.compile = function(element, attributes) {
        // Wird ausgeführt, wenn das Element kompiliert wird.
        element.css("color", "red");
        console.log('Run compile');

        var linkFunction = function($scope, element, atttributes) {
            // Wird ausgeführt, wenn das Element generiert und mit dem Scope verbunden wird.
                $scope.$parent.$watch('user', function(newValue, oldValue) {
                    console.log('change');
                }, true);
            console.log(atttributes.user);
            $scope.index = atttributes.index;
            
        }

        return linkFunction;
    }
    
    return directive;
});