【问题标题】:How to retrieve the stored value from goinstant如何从 goinstant 中检索存储的值
【发布时间】:2014-04-29 17:18:11
【问题描述】:

我做错了什么。我正在尝试获取即时存储的价值。我有一个带有用户名的人员房间。警报函数显示的值为“[object Object]”。这是我的代码:(我故意省略了脚本)。我在 goInstant 上提供了我的个人数据的快速屏幕截图,以供参考http://screencast.com/t/BtLqfrorg

<h2>Angular JS Test</h2>
<div ng-app="testapp" data-ng-controller="personCtrl">
        <input type="text" ng-model="userName"  />{{ userName }}
        <button type="submit" id="save" name="save" >Save</button>
    <script>
        var testApp = angular.module('testapp', ['goangular']);

        testApp.config(function($goConnectionProvider) {
            $goConnectionProvider.$set('https://goinstant.net/<mykey>/test');
        });

        testApp.controller('personCtrl', function($scope, $goKey) {
            // $goKey is available

            $scope.userName = $goKey('/person/userName').$sync();
            alert($scope.userName);

        });
    </script>
</div>

【问题讨论】:

    标签: goinstant goangular


    【解决方案1】:

    您的示例表明您希望 $scope.userName 是一个原始值(字符串)。事实上,它是一个模型。模型提供了一个简单的接口来更新应用程序的状态,并且在 GoAngular 中,该状态会自动保存到您的 GoInstant 应用程序中。

    您可以在 GoAngular 模型here 上找到更多文档。我认为一个可行的示例可能会有所帮助,所以我创建了一个Plunker让我们通过script.js

    angular
      .module('TestThings', ['goangular'])
      .config(function($goConnectionProvider) {
        $goConnectionProvider.$set('https://goinstant.net/mattcreager/DingDong');
      })
      .controller('TestCtrl', function($scope, $goKey) {
        // Create a person model
        $scope.person = $goKey('person').$sync();
    
        // Observe the model for changes
        $scope.$watchCollection('person', function(a, b) {
          console.log('model is', a.$omit());  // Log current state of person
          console.log('model was', b.$omit()); // Log the previous state of person
        });
    
        // After 2000 ms set the userName property of the person model
        setTimeout(function() {
          $scope.person.$key('userName').$set('Luke Skywalker');
        }, 2000);  
    
        // Set the userName property of the person model
        $scope.person.$key('userName').$set('Darth Vader');
      });
    

    【讨论】:

    • 太棒了!谢谢。
    • 如果我知道我想查询什么值,是否会使用 $get 来 $set userName?还是 $query 在这里最好?
    • 可能是$get,它更快:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 2013-11-01
    相关资源
    最近更新 更多