Run jQuery code after AngularJS completes rendering HTML

前端 未结 4 522
别跟我提以往
别跟我提以往 2020-12-02 08:08

In controller I get some JSON data using $http or $resource services. Then I write this data in $scope and AngularJS updates HTML structure of the page. My problem is that I

4条回答
  •  情话喂你
    2020-12-02 09:02

    Actually in this case the angular way is not the easy way but the only right way :)

    You have to write a directive and attach to the element you want to know the height of. And from the controller you $broadcast an event, the directive'll catch the event and there you can do the DOM manipulation. NEVER in the controller.

    var tradesInfo = TradesInfo.get({}, function(data){
        console.log(data);
        $scope.source.profile = data.profile;
        ...
    
        $scope.$broadcast('dataloaded');
    });
    
    
    directive('heightStuff', ['$timeout', function ($timeout) {
        return {
            link: function ($scope, element, attrs) {
                $scope.$on('dataloaded', function () {
                    $timeout(function () { // You might need this timeout to be sure its run after DOM render.
                        element.width()
                        element.height()
                    }, 0, false);
                })
            }
        };
    }]);
    

提交回复
热议问题