/**
 * This directive is very useful when generating content using ng-repeat.
 * It helps to create html elements with dynamically attached controllers.
 */
angular.module('app').directive('ngDynamicController', ['$compile', '$controller', function ($compile, $controller) {
    return {
        restrict: 'A',
        scope: true,
        link: function (scope, element, attrs) {

            var locals = {
                $scope: scope,
                $element: element,
                $attrs: attrs
            };

            var ctrl = scope.$eval(attrs.ngDynamicController);
            if (ctrl) {
                element.data('$Controller', $controller(ctrl, locals));
                try { scope.$digest(); } catch (err) { }
            }
        }
    };
}]);