Angular: after-render

by Lucas Duarte

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div ng-app="myApp" ng-controller="MyCtrl">
  <div after-render="missionCompled">element</div>
</div>

JavaScript

var myApp = angular.module('myApp',[]);
myApp.directive('afterRender', ['$timeout', function ($timeout) {
        var def = {
            restrict: 'A',
            terminal: true,
            transclude: false,
            link: function (scope, element, attrs) {
                $timeout(scope.$eval(attrs.afterRender), 0);  //Calling a scoped method
            }
        };
        return def;
    }]);

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

    $scope.missionCompled = function()
    {
         console.log('Done');   
    };
})