Javascript 在加载所有部分(ng包含)后运行函数

Javascript 在加载所有部分(ng包含)后运行函数,javascript,jquery,angularjs,templates,angularjs-ng-include,Javascript,Jquery,Angularjs,Templates,Angularjs Ng Include,加载页面中的所有部分后,如何运行jQuery行。 $(document.ready和$(document.load)没有成功 还有别的办法吗 app.controller('PageCtrl', function () { $(document).ready( function() { $(".left-nav").height( $('.content-area').height()); }); }); 在angular中加载的任何模板,首先将其存储在$templateCa

加载页面中的所有部分后,如何运行jQuery行。 $(document.ready和$(document.load)没有成功 还有别的办法吗

app.controller('PageCtrl', function () {
  $(document).ready( function() {
    $(".left-nav").height( $('.content-area').height());
  });
}); 

在angular中加载的任何模板,首先将其存储在$templateCache中,然后使用。 这是第二次直接从$templateCache获取它。 最好在应用程序运行块内的应用程序开始时加载所有模板

//app will be your current app name
app.run(['$templateCache','$q','$http', function($templateCache, $q, $http){
  var promises = [];
  promises.push($http.get('partials/views/template1.html',{ cache : $templateCache }));
  promises.push($http.get('partials/views/template2.html',{ cache : $templateCache }));
  promises.push($http.get('partials/views/template3.html',{ cache : $templateCache }));
  $q.all(promises, function (data) { 
    //write code here which want to load after all templates get loaded
    angular.element(".left-nav").css('height', angular.element('.content-area')[0].offsetHeight);
   //or if you have jquery file included the try below commented line
   //angular.element(".left-nav").css('height', angular.element('.content-area')[0].height();
 });
}]);
在angular$=angular.element中


希望这对您有所帮助。

您可以使用控制器

HTML:

说明: 通过保留一个包含已加载的包含数的变量,可以判断是否已加载所有包含数。
$scope.$watch
侦听第一个函数中返回的变量之间的差异,并通过调用第二个函数对其作出反应。 现在,
$scope.$watch
只会在角度摘要周期内触发
$timeout
调用循环,但可能不需要,我没有测试代码。 由于
PageCtrl
必须是应用程序的顶级控制器,$scope.includeLoaded可能对其他控制器可用,但我还是不确定。如果不起作用,请改用$rootScope


不过,潘卡帕卡的答案要好得多。只需我的2美分作为参考。

试试angular.element(document).ready(函数(){$(“.left nav”).height($(“.content area”).height();});抱歉@pankajparkar这不起作用:(你试过使用CSS flexbox来做这件事吗?@Joao我需要支持一些旧浏览器,所以我不能使用flexbox,但现在我使用的是flexbox本身:)我想有其他解决方案。谢谢你们的帮助。嘿@Syed我添加了答案。这是最好的答案。很好的答案,但我不会把jQuery放在app.run中。“角度方法”是将任何显示逻辑放入指令中。您可以在此处广播一个事件,该事件由设置高度的指令接收。@Joao答案已更新。.对于较小的DOM更改,我不认为我们可以包含一个指令。对于您的答案,我有向上投票权,但无法将其标记为答案,因为我有两个文件夹“partials”和“Views”。我用ng调用所有的partials include。首先是视图加载,然后是部分加载,所以问题就在这里。所以,这个代码对我来说不太合适。非常感谢您的帮助@pankajparkardid您尝试将其添加到控制器中而不是应用程序中。运行?为什么要在$timeout函数中增加IncludeLoaded?我没有测试代码,所以我不确定增量是否将在$digest周期内运行$timeout隐式调用$digest循环,这就是我使用它的原因。它很可能是不需要的。
<div data-ng-controller="includeCtrl" data-ng-include="'/partials/file.html'"></div>
app.controller('PageCtrl', function () {
    $scope.includesLoaded = 0;
    $scope.totalIncludes  = 10; //The total number of includes you have
    $scope.$watch(function($scope) { return $scope.includesLoaded },
        function(newValue, oldValue) {
            if(newValue === $scope.totalIncludes) {
                // Your code here
            }
        }
    );

}); 

app.controller("includeCtrl", ["$timeout",
    function($timeout) {
        var init = function() {
            $timeout(function() {
                $scope.includesLoaded++;
            }, 0);
        };
        init();
    }
]);