简体   繁体   中英

Pushing values to localstorage in angularjs not working

I am getting value from firebase when i push the value to localstorage empty srting is push how to fix it

my code :

    var app = angular.module("bucksbucketapplication", ['firebase', 'ngStorage']);
app.controller("bucksbucket_orders", function ($scope, $firebaseArray, $firebaseObject, $http, $localStorage) {
    var fbvalues = new Firebase("https://qwertyuiop.firebaseio.com/values");
    $scope.syncfromfbvalues = $firebaseArray(fbvalues);
    $scope.$storage = $localStorage.$default({
        "orders": []
    });
    $scope.data = $localStorage.orders;
    $scope.cloneItem = function (syncfromfbvalues) {
        $scope.$storage.orders.push({
            "price": syncfromfbvalues.id
        });
    }
});

and front my end code :

<html ng-app="bucksbucketapplication">

<head>
    <script src="angular.min.js"></script>
    <script src="firebase.js"></script>
    <script src="angularfire.min.js"></script>
    <script src="ngstr.js"></script>
    <script src="script.js"></script>

</head>

<body ng-controller="bucksbucket_orders">

         <li ng-repeat="message in syncfromfbvalues">{{message.greens}} <button class="btn btn-primary" data-ng-click="cloneItem(syncfromfbvalues)">to local</button></li>
</body>

</html>

and My Plunker Demo

Hi problem got fixed and this is the Updated Plunker Demo

reason as Rhumborl told in comment i worng send variable and now i got fixed

updated code

data-ng-click="cloneItem(message)" and then in script

$scope.cloneItem = function (message) {
        $scope.$storage.orders.push({
          "price": message.greens
        });
    }

Intstead of creating reference to $localStorage directly save values to the $localStorage.

$localStorage.orders = []
$scope.cloneItem = function (todo) {
    $localStorage.orders.push({
        "price": $scope.message
    });
}

I faced the same issue when I was working on it and it worked for me this way.

Use promise for asynchronous call. I made some changes into your example. Your messages object is undefined because of asynchronous operation. I added promise, so it's handled now.

Working example: http://plnkr.co/edit/I1Dyn0NmuLMDRYyS8ABU?p=preview

                $scope.getMessages = function () {
                var arr, fbvalues = new Firebase("https://qwertyuiop.firebaseio.com/values");
                arr = $firebaseArray(fbvalues);
                arr.$loaded().then(function (msg) {
                    console.log('Mymsg', msg)
                    return $scope.message = msg;
                });
            };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM