Pregunta

I'm doing something wrong. I'm attempting to get the stored value I have in goinstant. I have a person room with a userName. The value the alert function displays is "[object Object]". Here is my code: (I left out the scripts intentionally). I provided a quick screen shot of my person data on goInstant for reference 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>
¿Fue útil?

Solución

Your example would indicate that you expect $scope.userName to be a primitive value (a string). It is in fact, a model. Models provide a simple interface for updating the state of your application, and in GoAngular, that state is persisted to your GoInstant App auto-magically.

You can find more documentation on the GoAngular Model here. I thought a working example might help, so I've created a Plunker. Let's work through the 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');
  });
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top