AngularJS: callback after render (work with DOM after render)

前端 未结 5 838
长发绾君心
长发绾君心 2020-12-08 02:26

How can run a method $scope.myWork() after render template? I want to set the $scope.value and after that I need to change something with JQuery (e

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 03:01

    I use terminal and transclude in a attribute directive to call a scoped method after a model is updated and view is rendered (in my case to resize a iframe after a $Resource.query):

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

    The $timeout is black magic. It should be possible to declare the JS method as the attribute value and $parse it.

    So I use it in a ng-repeat (in my case a tree is rendered recursively):

提交回复
热议问题