BCJTI / ng2-cookies

Simple library to deal with cookies in Angular2
64 stars 31 forks source link

Error Cookie.check('cookieName'); #33

Closed pogran closed 7 years ago

pogran commented 7 years ago

This method not work . http://prntscr.com/e602nz

Bigous commented 7 years ago

Hi @pogran Tks for your effort to make this library better!

I'm not able to reproduce the problem here (windows 10 with chrome 55).

I've started a new project with angular-cli:

npm install -g angular-cli
ng new ng2-cookies-test
cd ng2-cookies-test
npm install --save ng2-cookies
code .
ng serve

And edited the app.component.html to be like this:

<h1>
  {{title}}
</h1>

<table>
  <thead>
    <tr>
      <td colspan="2">Cookies:</td>
    </tr>
    <tr>
      <th>Name</th>
      <th>Value</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let cookie of keys">
      <td>{{cookie}}</td>
      <td>{{cookies[cookie]}}</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>
        <label for="iName">Name:</label>
        <span><input type="text" id="iName" [(ngModel)]="cName"></span>
      </td>
      <td>
        Value:
        <span><input type="text" [(ngModel)]="cValue"></span>
        <input type="button" value="Add" (click)="addCookie(cName, cValue)">
      </td>
    </tr>
    <tr>
      <td colspan="2">
        Name:
        <span><input type="text" [(ngModel)]="rName"></span>
        <input type="button" value="Remove" (click)="removeCookie(rName)">
        <input type="button" value="Remove All" (click)="removeAll()">
      </td>
    </tr>
    <tr>
      <td colspan="2">
        Name:
        <span><input type="text" [(ngModel)]="checkName"></span>
        <input type="button" value="Check" (click)="checkCookie(checkName)">
      </td>
    </tr>
  </tfoot>
</table>

And the app.component.ts to be like that:

import { Component } from '@angular/core';
import { Cookie } from 'ng2-cookies';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
  cookies: Object;
  keys: Array<string>;
  cName: string;
  cValue: string;
  rName: string;
  checkName: string;

  constructor() {
    this.update();
    console.log(this.cookies);
  }
  update() {
    this.cookies = Cookie.getAll();
    this.keys = Object.keys(this.cookies);
  }
  addCookie(cName: string, cValue: string) {
    console.log('Adding: ', cName, cValue);
    Cookie.set(cName, cValue);
    this.update();
  }
  removeCookie(rName: string) {
    console.log('Removing: ', rName);
    Cookie.delete(rName);
    this.update();
  }
  removeAll() {
    console.log('Removing all cookies');
    Cookie.deleteAll();
    this.update();
  }
  checkCookie() {
    console.log('Checking: ', this.checkName);
    console.log(Cookie.check(this.checkName));
    window.alert('Check cookie ' + this.checkName + ' returned ' + Cookie.check(this.checkName));
  }
}

And when I open the browser at http://localhost:4200, I can add, delete and check cookies with no problem.

Bigous commented 7 years ago

All wright, I'm assuming that it's working for you. Any news, let us know.