在完成加载angular js Scope元素、Directives和Http请求后是否发生任何事件

Is there any event after completly loading of angular js Scope elements and Directives and Http requests

本文关键字:请求 Http 是否 事件 任何 Directives 加载 angular js 元素 Scope      更新时间:2024-06-08

我想在http请求将数据发送到控制器后以及在数据绑定到控制器后调用一个javascript函数。有这样的角度法吗?

$timeout将是您知道控制器和绑定已完成的最佳选择。然而,这需要您处于Angular生命周期中。没有事件可以知道HTTP请求是挂起的或完成的,您需要跟踪感兴趣的请求的状态。

如果你试图从Angular生命周期之外调用一个普通的JavaScript函数,我会质疑你想做什么

您可以通过使用标志来实现它。

  1. 在控制器上放置ng if="somevariableflag"

http请求成功后,将此变量设置为true。现在将进行dom渲染。。要知道ng重复是否完成,请创建类似的指令

<div ng-controller="Ctrl">
  <div ng-repeat="test in testing" repeat-completed>
    {{test}}
  </div>
</div> 
angular.module('myApp', [])
.directive('repeat-completed', function() {
  return function(scope, element, attrs) {
    if (scope.$last){
      //do whatever you want to do
    }
  };
});

您可以通过使用"$watch"服务来实现这一点。

$watch服务持续观察模型。每当模型的值发生变化时,它就会运行侦听器函数

    Syntax: scope.$watch('watchExpression ( this can be your model )' , function() {
   // this is a listener function you can define here
   // actions to perform
})

请点击下面的链接。https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch