完全加载角度js范围元素和指令以及Http请求后是否有任何事件

时间:2015-02-27 05:37:19

标签: javascript angularjs angularjs-directive angularjs-scope angular-ui-router

我想在http请求发送数据到控制器之后和数据绑定到控制器之后调用javascript函数。有没有这样的角度方法?

3 个答案:

答案 0 :(得分:0)

知道控制器和绑定完成后,

$ timeout是你最好的选择。但是,这需要您使用Angular生命周期。没有事件要知道HTTP请求正在等待或完成,您需要跟踪您感兴趣的请求的状态。

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

答案 1 :(得分:0)

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

  1. on controller put ng-if =“somevariableflag”
  2. 关于http请求成功将此变量设置为true。现在将进行dom渲染..要知道ng-repeat是否已完成,请创建一个指令,如

    <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
        }
      };
    });
    

答案 2 :(得分:0)

您可以使用“$ watch”服务来实现此目的。

$ watch service持续观看模特。只要模型的值发生变化,它就会运行监听功能

    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#$表