本地存储即使设置也未定义。离子的

时间:2016-08-30 09:02:21

标签: javascript angularjs cordova ionic-framework

我正在效仿这个例子, https://www.thepolyglotdeveloper.com/2015/02/make-facebook-mobile-app-ionic-framework/

我在app.js文件中声明了 ng-storage 。而且:

app.controller('facebookCtrl', ['$cordovaOauth','$scope','$localStorage',function ($cordovaOauth,$scope,oauthService, $localStorage) 
{
$scope.login = function() 
    {
        $cordovaOauth.facebook("1234566878", ["email", "user_location","public_profile"]).then(function(result) {
            $localStorage.currentUser  = result.access_token;

            $http.defaults.headers.common.Authorization = 'Bearer ' +   $localStorage.currentUser ;
            console.log ('acess token is ' + result.access_token)


        }, function(error) {
            alert("There was a problem signing in!  See the console for logs");
            console.log(error);
        });
    };



 }]);

我一直在

  

无法设置属性' currentUser'未定义的

我尝试在开始时$localStorage.set('currentUser', result.access_token);

启动它

仍然无法正常工作

1 个答案:

答案 0 :(得分:2)

我用这种方式来定义localstorage并且它对我有用

app.controller('facebookCtrl', ['$cordovaOauth', '$scope', '$localStorage', function($cordovaOauth, $scope, oauthService, $localStorage) {
    $scope.$storage = $localStorage.$default({
        currentUser = null;
    });
    $scope.login = function() {
        $cordovaOauth.facebook("1234566878", ["email", "user_location", "public_profile"]).then(function(result) {
            $scope.$storage.currentUser = result.access_token;

            $http.defaults.headers.common.Authorization = 'Bearer ' + $localStorage.currentUser;
            console.log('acess token is ' + result.access_token)


        }, function(error) {
            alert("There was a problem signing in!  See the console for logs");
            console.log(error);
        });
    };



}]);