2016-08-24 6 views
0

私はAngularJSでES6を使用しており、this guideの後にいます。サービスをクラスとして定義することを推奨します。私は$は、cookiestoreサービスを注入すると、このようなクラスに取り付けるてるサービス定義で

export default class Authentication { 
    constructor($cookieStore) { 
    this.$cookieStore = $cookieStore; 
    } 

    setAuthenticatedAccount(account) { 
    this.$cookieStore.put(JSON.stringify(account), 'authenticatedAccount'); 
    } 

    isAuthenticated() { 
    return !!this.$cookieStore.get('authenticatedAccount'); 
    } 
} 

私は、ユーザーがログインしているかどうかを反映するために、私のナビゲーションバーを更新したい、どの私は$watchでやっている:

Cannot read property '$cookieStore' of undefined

​​

これは、次のエラーが発生します。

+0

どのようにサービスを登録しないで注入されていることを確認してください?あなたの例は、クラス定義のみを示しています。 – zeroflagL

答えて

0

このエラーは通常、コントローラに適切な依存関係を注入していない場合に発生します。

だけ$cookieStoreがpropertlyあなたの角度コントローラ

myApp.controller('myCtrl', ['$scope', '$cookieStore', function ($scope, $cookieStore) { 
... 
... 

point to note
The $cookieStore service is deprecated. Please use the $cookies service instead.

関連する問題