Run jQuery code after AngularJS completes rendering HTML

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

    Olivér's answer is good, but has an issue: if you forget to broadcast the event, your javascript will not run whereas your data might have changed. Another solution would be to watch for changes on the scope, for instance:

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

    This way the javascript functions are called every time the data changes without any need for a custom event.

提交回复
热议问题