0

我创建了一个指令,该指令只是将存储在.html中的一些div标签转换为单个元素。我注意到该指令只在页面重新加载时编译,当路由改变时指令不会被激活,我知道你可以使用$ routeChangeSucess或$ scope.watch来检测路由改变,但在我的情况下,我没有使用在这个目录中的链接函数,我只使用templateUrl。在路由改变时重新加载/重新运行此指令的最佳方式是什么?下面是我的代码;重新运行路线变化中的所有角度对象指令(不仅链接功能)

stockfront.factory('directiveConfig', function(){ 
factory = {}; 
factory.returnConfig = { 
    scope : true, 
    restrict: "E", 
    replace: true, 
    templateUrl : "", 
    template : "", 
    transclude : true, 
    link : "", 
    combine: "" 
}; 

return factory; 
}); 

stockfront.directive("mainDiv", function(directiveConfig){ 

directiveConfig.returnConfig.templateUrl = "./template/mainDiv.html"; 

return directiveConfig.returnConfig; 

}); 

这就是我想@Valery科兹洛夫:

stockfront.run(function($rootScope) { 
    $rootScope.routeChange = false; 
    $rootScope.$on("$routeChangeSuccess", function() { 
    // handle route changes 
    $rootScope.routeChange = true; 
    }); 
}); 

然后在html:

<main-div ng-if="$root.routeChange"><main-div> 

回答

0

店某处$ rootScope stateTransition状况和使用下面的代码片段:

<my-dir ng-if="!$root.transitionInProgress"></my-dir> 
+0

@瓦列里·科兹洛夫这似乎没有工作。 –

0

我刚刚意识到我在检查代码后出现错误....当另一个dirctory被触发时,工厂的returnConfig属性在routechange上被重写。该解决方案是宣布工厂为对象然后添加returnConfig为原型的属性,它使得它独特的每一个指令instatiation

stockfront.factory('directiveConfig', function(){ 

//constructor 
function directiveConfig(){ 

} 

directiveConfig.prototype.returnConfig = function(){ 
    return { 
     scope : true, 
     restrict: "E", 
     replace: true, 
     templateUrl : "", 
     template : "", 
     transclude : true, 
     link : "", 
     combine: "" 
    }; 
}; 

return directiveConfig; 
}); 


stockfront.directive("mainDiv", function(directiveConfig){ 
    var obj = new directiveConfig() 
    var dirDefault = obj.returnConfig(); 
    dirDefault.templateUrl = "./template/mainDiv.html"; 

return dirDefault; 

});