Run jQuery code after AngularJS completes rendering HTML

前端 未结 4 523
别跟我提以往
别跟我提以往 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 08:50

    Another suggestion to work with JQuery. Had to work this through for a grid that was generated in a directive. I wanted to scroll to a specific row in the grid. Use $emit to broadcast from directive to parent controller:

    In Controller:

        ['$timeout',function($timeout){
    ...
     $scope.$on('dataloaded', function () {
                $timeout(function () { // You might need this timeout to be sure its run after DOM render.
                    $scope.scrollToPosition();
                }, 0, false);
            });
            $scope.scrollToPosition = function () {
                var rowpos = $('#row_' + $scope.selectedActionID, "#runGrid").position();
                var tablepost = $('table', "#runGrid").position();
                $('#runGrid').scrollTop(rowpos.top - tablepost.top);
            }
    

    In directive

    .directive('runGrid',['$timeout', function ($timeout) {
            // This directive generates the grip of data
            return {
                restrict: 'E',  //DOM Element
                scope: {    //define isolated scope
                    list: '=',   //use the parent object
                    selected: "="
                },
    
                templateUrl: '/CampaignFlow/StaticContent/Runs/run.grid.0.0.0.0.htm',  //HTML template URL
    
                controller: ['$scope', function ($scope) {  //the directive private controller, whith its private scope
                    //$scope.statusList = [{ data_1: 11, data_2: 12 }, { data_1: 21, data_2: 22 }, { data_1: 31, data_2: 32 }];
                    //Controller contains sort functionallity
    
                    $scope.sort = { column: null, direction: 1 }
                    $scope.column = null;
                    $scope.direction = "asc";
                    $scope.sortColumn = function (id) {
                        if(id!=$scope.column) {
                            $scope.column = id;
                            $scope.direction = "asc";
                        } else {
                            $scope.column = null;
                        }
                    }
                    $scope.toggleDir = function () {
                        $scope.direction = ($scope.direction == "asc") ? "desc" : "asc";
                    }
                   $scope.$emit('dataloaded');
                }]
    
    
            };
        }])
    

    And this is a snippet of the grid directive html template:

     

    the list and selected parameters are injected from the html that is using the directive

    
    

    提交回复
    热议问题