【问题标题】:How to not call the directive everytime i reload a page每次我重新加载页面时如何不调用指令
【发布时间】:2015-08-20 19:35:03
【问题描述】:

我的指令有问题,请帮我解决这个问题。 这是我的指令,它改变了 css 样式,但是每次当我转到另一个页面时,所有这些重新加载,我不想要这个。

directive('layoutSettings', function ($http) {
        return {
            restrict: 'A',
            link: function (scope, element, $rootScope) {
                    $rootScope.selectedLayout = {};
                    $http.get('http://localhost/obp_shop/api/LayoutSettings/GetValue').success(function(data) {
                        $rootScope.selectedLayout = data;
                        console.log($rootScope.selectedLayout);
                        $(element).css({ 'background-color': $rootScope.selectedLayout[0].value });
                        $(element).css({ 'background-image': 'url(data:image/jpeg;base64,' + $rootScope.selectedLayout[1].value + ')' });
                        $('.btn-primary').css({ 'background-color': $rootScope.selectedLayout[2].value });
                        $('.btn-default').css({ 'background-color': $rootScope.selectedLayout[2].value });
                        $('btn-primary').css({ 'color': $rootScope.selectedLayout[3].value });
                        $('.btn').css({ 'font-size': $rootScope.selectedLayout[4].value });
                        $('h1,h2,h3,h4,h5,h6').css({ 'color': $rootScope.selectedLayout[5].value });
                        $('h1,h2,h3,h4,h5,h6').css({ 'font-size': $rootScope.selectedLayout[6].value });
                        $(element).css({ 'color': $rootScope.selectedLayout[7].value });
                        $(element).css({ 'font-size': $rootScope.selectedLayout[8].value });
                    });
                }
        };
    });

这个指令在索引页面的主体上,我可以在其中获取其他页面,因为我使用的是 ui 路由器。

<body class="container" ng-app="routerApp" style="padding-top: 50px;" layout-settings>
<div ui-view cg-busy="{promise:promise}"></div>
</body>

我怎样才能只使用这个指令一次? 谢谢。

【问题讨论】:

    标签: javascript css angularjs angular-ui-router


    【解决方案1】:

    我的问题已通过使用 ui-view 将指令放入 div 中得到解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-28
      • 2021-06-10
      • 2018-01-23
      • 1970-01-01
      • 2020-12-19
      • 2021-11-13
      • 2020-06-17
      • 2014-02-16
      相关资源
      最近更新 更多