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; 

});