简体   繁体   English

使用templateURL的角度指令的渲染顺序

[英]Rendering Order of Angular Directives with templateURL

I have the following scenario, simplified in jsFiddle 我有以下情况,在jsFiddle中简化了

I'm trying to run some code in the parent wizard element, after all steps in it rendered. 在呈现所有步骤之后 ,我尝试在父向导元素中运行一些代码。 in this context, console logs are generated as they should: 1. step1. 在这种情况下,将按如下方式生成控制台日志:1. step1。 2. step2. 2. step2。 3. wizard. 3.向导。

However, when i change the template of any step to a templateURL with an actual HTML, the order is getting messed up and i cannot figure out a way to make sure that functionality in wizard is running last. 但是,当我将任何步骤的模板更改为带有实际HTML的templateURL时,顺序变得一团糟,并且我无法找出一种方法来确保向导中的功能最后运行。

I have tried this concept in the wizard directive but it still doesn't work for me. 我已经在向导指令中尝试过这个概念,但是它对我仍然不起作用。

$timeout(function () {
    //DOM has finished rendering
});

Is there a known solution for this? 是否有已知的解决方案?

I made a jsfiddle with the answer. 我用答案做了一个jsfiddle。 http://jsfiddle.net/KyEr3/1746/ However, I don't see why you would need to do this. http://jsfiddle.net/KyEr3/1746/但是,我不明白为什么您需要这样做。 Basically, you could have a service track the init count to ensure both directives have initialized. 基本上,您可以让服务跟踪初始化计数,以确保两个指令都已初始化。 Watch the value of this tracker, and test it when it changes. 观察此跟踪器的值,并在其更改时对其进行测试。 Kazam. 卡赞

Because services are singletons and persistent, you may want to reset the count to zero when the wizard directive initializes. 因为服务是单例的并且是持久性的,所以您可能希望在初始化向导指令时将计数重置为零。

Here's the code: 这是代码:

    var myApp = angular.module('myApp', []);

myApp.service("initState", function(){
return{
    initCount:0,
  finishStep:function(){
    this.initCount++;
  }
}

});

myApp.directive("wizard",function(initState){
    return {
    link: function($scope){
        $scope.$watch(function(){
        return initState.initCount;
      }, function(){
      console.log('init count', initState.initCount );
        if( initState.initCount === 2){
            console.log('link on wizard');
        }
      })

    },
        restrict: "E",
        template: "<step-1></step-1><step-2></step-2>"
    }
});

myApp.directive("step1", function(initState){
    return {
    link: function(){
        console.log('link on step1');
      initState.finishStep();
    },
        restrict: "E",
        template: "<h1>Step1</h1>"
    }
});

myApp.directive("step2", function(initState){
    return {
    link: function(){
        console.log('link on step2');
       initState.finishStep();
    },
        restrict: "E",
        template: "<h1>Step2</h1>"
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM