简体   繁体   English

在AngularJS中将值推送到本地存储不起作用

[英]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 当我将值推送到本地存储时,我正在从Firebase中获取值存储空存储正在推送如何修复它

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 您好问题已解决,这是更新的Plunker演示

reason as Rhumborl told in comment i worng send variable and now i got fixed 正如Rhumborl在评论中告诉我的原因,我会发送变量,现在我已经解决了

updated code 更新的代码

data-ng-click="cloneItem(message)" and then in script data-ng-click="cloneItem(message)" ,然后在脚本中

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

Intstead of creating reference to $localStorage directly save values to the $localStorage. 与其创建对$ localStorage的引用,不如直接将值保存到$ 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. 对异步调用使用promise。 I made some changes into your example. 我对您的示例进行了一些更改。 Your messages object is undefined because of asynchronous operation. 由于异步操作,您的message对象未定义。 I added promise, so it's handled now. 我增加了诺言,所以现在就处理了。

Working example: http://plnkr.co/edit/I1Dyn0NmuLMDRYyS8ABU?p=preview 工作示例: 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;
                });
            };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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