无法在Controller或Service中设置cookie

时间:2015-11-13 18:43:31

标签: javascript angularjs cookies

我正在尝试实现cookie合规模式,让用户接受或拒绝。当用户单击“接受”时,我使用Carhartl(https://github.com/carhartl/jquery-cookie)的jQuery插件设置cookie。当我在控制台中使用$.cookie('cookieCompliant', 'true);时,cookie设置正常。但是,在我的控制器中使用时,cookie不会设置。我尝试过vanilla JS和Angular的$cookie,但它们都没有用。对此有限制吗?我也无法让alert()console.log工作。

MainController

Main.controller("MainController", ['$scope', 'MainFactory', 'CookieFactory', function($scope, MainFactory, CookieFactory){

$scope.cookies = new CookieFactory();

$scope.siteSettings = new MainFactory();

$scope.agreeCookieCompliance = function(){
    $.cookie("cookieCompliant", "true");
    $('#cookieModal').modal("hide")
}

$scope.declineCookieCompliance = function(){
    window.location.href = "https://encrypted.google.com/";
}

$scope.checkCookieCompliance = function(){
    if($.cookie('cookieCompliant') === undefined){

        $('#cookieModal').modal("show");            
    }
}

$scope.checkCookieCompliance();

}]);  

index.php (modal)

<div class="modal fade" id="cookieModal"tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static" data-keyboard="false" style="display: block; padding-left: 17px;">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title">This site uses cookies</h4>
          </div>
          <div class="modal-body">
            <p>This site uses cookies to give you the best possible experience. By clicking "Accept" you agree to the use of cookies.</p>
          </div>
          <div class="modal-footer">
            <a class="pull-left" style="line-height: 2.5;" href="http://www.whatarecookies.com/" target="_blank">What are Cookies?</a>
            <button type="button" ng-click="acceptCookieCompliance()" class="btn btn-success" data-dismiss="modal">Accept</button>
            <button type="button" ng-click="declineCookieCompliance()" class="btn btn-danger">Decline</button>
          </div>
        </div>
      </div>
    </div>

注意:我没有使用CookieFactory,它只是在那里,因为我疯狂地寻找解决方案。

0 个答案:

没有答案