3

私は何か間違ったことをしています。goinstant に保存されている値を取得しようとしています。userName の個人ルームがあります。アラート機能が表示する値は「[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 に答える