0

Хочу добавить куки баннер на сайт, но почему-то ничего не отображается, в чем проблема?

angular.module('app', ['ngCookies'])
    .directive('consent', function ($cookies) {
        return {
            scope: {},
            template:
                 '<div style="position: relative; z-index: 1000">' +
                 '<div style="background: #ccc; position: fixed; bottom: 0; left: 0; right: 0" ng-hide="consent()">' +
                 ' <a href="" ng-click="consent(true)">I\'m cookie consent</a>' +
                 '</div>' +
                 '</div>',
            controller: function ($scope) {
                var _consent = $cookies.get('consent');
                $scope.consent = function (consent) {
                    if (consent === undefined) {
                        return _consent;
                    } else if (consent) {
                        $cookies.put('consent', true);
                        _consent = true;
                    }
                };
            }
        };
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-cookie/4.1.0/angular-cookie.js"></script>
<div consent></div>

1 ответ 1

1

В вашем примере подключена не та библиотека для работы с cookie.

https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-cookies.js - вот это куки от ангуляра.

Я не понимаю логики работы вашего баннера, но куки устанавливаются.

Пример на jsfiddle.

angular.module('app', ['ngCookies'])
  .directive('consent', function($cookies) {
    return {
      scope: {},
      template: '<div style="position: relative; z-index: 1000">' +
        '<div style="background: #ccc; position: fixed; bottom: 0; left: 0; right: 0" ng-hide="consent()">' +
        ' <a href="" ng-click="consent(true)">I\'m cookie consent</a>' +
        '</div>' +
        '</div>',
      controller: function($scope) {
        var _consent = $cookies.get('consent');
        console.log(_consent);
        $scope.consent = function(consent) {
          console.log(consent);
          if (consent === undefined) {
            return _consent;
          } else if (consent) {
            $cookies.put('consent', true);
            _consent = true;
          }
        };
      }
    };
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-cookies.js"></script>
<div ng-app="app">
  <div consent></div>
</div>

2
  • когда открываю jsfiddle. Он пуст(
    – olliJes
    17 фев 2020 в 14:13
  • @olliJes Конечно пустой. Я добавил логов. Внимательно смотрите на свой код и на логи в devTools. 18 фев 2020 в 10:32

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.