角:当所有的指令都被载入控制器运行功能 [英] Angular: running function in controller when all directives are loaded

查看:117
本文介绍了角:当所有的指令都被载入控制器运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拿出一些code,让我在控制器中运行的功能,但只有一次整个DOM设置和准备(包括指令链接功能运行等)。

I'm trying to come up with some code which allows me to run a function in the controller but only once the whole dom is setup and ready (including the directives link function run etc.).

我目前CTRL /服务,并通过$ rootScope广播指令之间的通信。在控制器装载时第一广播没有被由指令拾取。原因当然是该指令链接功能之前,控制器负荷运行的。我读过,人们使用$超时这些调用推荐上这么几个类似的问题。不幸的是这并不总是工作,我不希望有很多的$超时调用弄乱我CTRL /服务。因此,我正在寻找另一种解决我的问题。

I'm currently communicating between ctrl/service and the directive via $rootScope broadcasts. The first broadcast at the time of the controller loading is not being picked up by the directive. The reason is of course that the controller loads before the directive link function runs. I've read a few similar questions on SO where people recommended on using $timeout for these calls. This unfortunately doesn't always work and I don't want to clutter my ctrl/services with lots of $timeout calls. Therefore I'm looking for another solution to my problem.

通讯模式如下:

1)负责人告诉服务,以prepare一些数据(通过服务函数调用)

1.) Controller tells Service to prepare some data (via function call in service)

2)服务告诉指令来显示数据(通过广播)

2.) Service tells directive to display data (via broadcast)

3)指令显示数据...或者不在我的情况:(

3.) Directive displays data ...or not in my case :(

编辑:

由于时机是在我的应用程序至关重要,我基本上寻找一种方式尽快所有角分量已完成加载启动控制器的功能。在控制器功能将显示由一个范围的变量赋值的内容。同时它将开始抽空。我当然只能开始做,一旦指令被加载,否则tmining错误或指令还没有准备好来显示内容等。

As timing is essential in my app, I'm basically looking for a way to initiate a function in the controller as soon as all angular components have finished loading. That function in the controller will display content by assigning a value to a scope variable. At the same time it will start taking the time. I can of course only start doing that once the directives are loaded, otherwise the tmining is wrong or the directive is not yet ready to display content etc.

我已经通过由本·纳德尔,这基本上表明指令是如何加载的博客文章阅读。我希望能设置,其中最后加载,所以我可以从那里触发成品装载的外部指令。不幸的是,这并不只要任何内部指令的使用templateUrl工作。
http://www.bennadel.com/blog/2603-directive-controller-and-link-timing-in-angularjs.htm

I've read through a blog post by Ben Nadel, which basically shows how directives are loaded. I was hoping to setup an outer directive which loads last so I can trigger the finished loading from there. Unfortunately that doesn't work as soon as any of the inner directives use a templateUrl. http://www.bennadel.com/blog/2603-directive-controller-and-link-timing-in-angularjs.htm

推荐答案

使用$超时将是可怕的。不要那样做。你不能定义服务器呼叫要去多久服用。

Using $timeout would be terrible. Don't do that. You can't define how long a server call is going to take.

我会建议使用这种模式:

I would recommend using this pattern:


  • 有控制器使用服务来加载一些数据,并具备
    许在控制器返回数据分配给一个范围可变。

  • 传递范围的变量到你的指令。

  • 设置在该指令链接功能的手表,在加载时它将从不确定到所需的值。完成!

//在你的控制器

YourService.all().then(function(data) {
  $scope.data = data;
});

//在视图

<some-directive your-data="data"></some-directive>

//在你的指令

// in your directive

angular.module('blah.directives').directive('someDirective', function() {
    return {
      scope: {
        yourData: '='
      },
      link: function(scope, element, attrs) {

        var watcher = scope.$watch('yourData', function() {
          if(scope.yourData === undefined) return;

          // at this point your data will be loaded, do work

          // optionally kill watcher at this point if it's not going to update again
          watcher();
        });
      }
    }
  });

这篇关于角:当所有的指令都被载入控制器运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆