Search code examples
javascriptangularjsangularjs-directiveangularjs-scope

AngularJs $compile finish / end event


Hi I would like to know, if there is a way to get something like promise (i guess it will be postLink in this case) on $scope. i an unanswered question here: AngularJS: how to know when the $compile has finished? which is very similar to my question,

but i wonder if someone can help me understand how can i hook the postLink event:

i have the following code:

var compiledEl =
$compile("<link data-ng-repeat='stylesheet in injectedStylesheets' data-ng-href='{{stylesheet.href}}' rel='stylesheet' />");
head.append(compiledEl(scope));

so i don't know where to add promise.resolved(true); cause i don't see the postLink. how can i know when the $compile comes to an end. Thanks


Solution

  • You can make yourself a directive:

    app.directive('compileCallback',function(){
      return {
        priority: 1001, // make sure it's the last to run
        link: function (scope, element, attrs){
          scope.$eval(attrs.compileCallback);
        }
      }
    });
    

    Inside your template:

    <link data-ng-repeat='stylesheet in injectedStylesheets' data-ng-href='{{stylesheet.href}}' rel='stylesheet' compile-callback="promiseResolve()" />