3

我做错了什么。我正在尝试获取即时存储的价值。我有一个带有用户名的个人房间。警报函数显示的值是“[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>
4

1 回答 1

4

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

您可以在此处找到有关 GoAngular 模型的更多文档。我认为一个工作示例可能会有所帮助,所以我创建了一个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');
  });
于 2014-04-29T20:31:20.773 回答