侦听特定的$ includeContentLoaded

时间:2014-08-25 08:31:26

标签: angularjs

每次更新$includeContentLoaded时都会触发{p> ngInclude

$scope.$on('$includeContentLoaded', function() {
  // Emitted every time the ngInclude content is reloaded
});

我想做的是听一个特定的ngInclude加载,就像回调一样:

$scope.includePath('/path/to/iclude', function() {
  // Fired when the includePath ngInclude have finished loading
}

这可能吗?

3 个答案:

答案 0 :(得分:5)

您可以使用" onload"标记:

<div ng-include="url" onload="test(url)">
</div> 

并在范围函数中检查url,例如:

$scope.test = function(url){
    switch(url) {
        case '/path/to/iclude':
          /*******YOUR CODE***********/
          break;
    }
} 

答案 1 :(得分:3)

在Angular 1.3中,你有:

$rootScope.$on('$includeContentLoaded',function(event,url){
  if (url == 'YourDesiredPath') {
    // do something
  }
});

答案 2 :(得分:2)

Angular有自己的方法:$ includeContentLoaded

    $scope.$on('$includeContentLoaded', function(eve,uri) {
       if(uri == 'views/templates/sample.html'){
        // do something
     });   
   }