【问题标题】:AngularJs override templateUrl functionAngularJs 覆盖 templateUrl 函数
【发布时间】:2016-11-08 18:35:09
【问题描述】:

因为每个部分模板都被缓存了,我不想这样做

.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();
       });

【问题讨论】:

标签: angularjs


【解决方案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);
            });
        }
    }
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2015-03-12
    相关资源
    最近更新 更多