2016-11-08 108 views
0

因为每一个局部模板缓存,我不,不想我这样做AngularJs覆盖templateUrl功能

.directive('myMessages', [ function() { 
    return { 
     restrict : 'E', 
     templateUrl : 'wwwroot/base/messages/ui.partial.messages.show.html?v' + Date.now(), 

,才有可能在全球范围覆盖templateUrl功能,并在末尾添加日期?

我也尝试从论坛这两种解决方案,但他们永远不会触发:

$rootScope.$on('$routeChangeStart', function(event, next, current) { 
      $templateCache.remove(current.templateUrl); 
     }); 

     $rootScope.$on('$viewContentLoaded', function() { 
      $templateCache.removeAll(); 
     }); 
+0

[与UI的路由器AngularJS禁用模板缓存](http://stackoverflow.com/questions/23589843/disable-template-caching-in-angularjs-with-ui-router) –

+0

的可能的复制你把那些代码放在哪里?我在我的app.run函数中或多或少地做了同样的事情,并且它可以正常工作,每次运行视图时都会加载它。 –

回答

1

如果我没看错,你要使用你的指令,因为每个页面顶部的一面旗帜。

这将是最好的,如果你能说服你的后端返回给你一个JSON而不是部分HTML,但我知道这并不总是会发生。

而不是使用templateUrl来实现这一点,我会建议使用$http.get并加载它的指令。

.directive('myMessages', [ function() { 
    return { 
     restrict : 'E', 
     template : '<div></div>', 
     link: function(scope, element, attrs){ 
      $http.get('wwwroot/base/messages/ui.partial.messages.show.html?v' + Date.now()).then(function(response.data) { 
       // if your html contains angular syntax, use $compile 
       element.html(response.data); 
       $compile(element.contents())(scope); 

       // if your html doesn't contain angular syntax, use $sce 
       // your template needs to be <div ng-bind-html="message"></div> 
       scope.message = $sce.trustAsHtml(response.data); 
      }); 
     } 
    } 
]);