我做错了什么。我试图获得我在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>
答案 0 :(得分:4)
您的示例表明您希望$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');
});