【发布时间】:2016-03-03 19:36:35
【问题描述】:
Plunker:http://plnkr.co/UwPmy7
基本上,我的 Ionic 应用程序中有一个页面,我希望用户能够将信息输入到文本框中,然后根据他们的特定用户 ID 将该信息同步到 firebase 数据库。
我有 2 个工厂,它们分别为用户信息生成身份验证令牌和 firebase 对象:
// Returns Firebase authenticator
.factory('Auth', ['$firebaseAuth',
function($firebaseAuth) {
var ref = new Firebase('https://luminous-torch-5887.firebaseio.com/')
return $firebaseAuth(ref);
}
])
// Returns information for specified user's sale
.factory("UserSale", ["$firebaseObject",
function($firebaseObject) {
return function(userID) {
var ref = new Firebase('https://luminous-torch-5887.firebaseio.com/users/');
var userSale = ref.child(userID);
return $firebaseObject(userSale);
}
}
]);
我的登录和显示选项卡控制器从生成一个稍后用于登录的身份验证令牌开始:
// Manages user-related information
.controller('AccountCtrl', ['$scope','Auth','$firebaseObject','UserSale',
function($scope, Auth, $firebaseObject, UserSale) {
// Create authenticator
$scope.auth = Auth;
// Set user's credentials when logged in
$scope.auth.$onAuth(function(authData) {
$scope.authData = authData;
});
// More functions...
这是我的登录功能,也用于在用户登录时对用户的数据进行三向绑定:
// AccountCtrl
$scope.authWithPassword = function() {
Auth.$authWithPassword({
// Retrieve user input
email: $scope.email,
password: $scope.password
}).then(function(authData) {
// When logged in, bind user data
UserSale(authData.uid).$bindTo($scope, 'userSale');
console.log('Logged in as: ' + authData.uid);
}).catch(function(error) {
console.error('Authentication failed: ' + error);
});
};
然后可以在 HTML 界面中修改数据:
<ion-view view-title="Your Sale">
<ion-content ng-controller="AccountCtrl">
<!-- Show fields only when logged in -->
<div ng-show="authData">
<label class='item item-input'>
<span class='input-label'>Title</span>
<input type='text' ng-model='userSale.title'>
</label>
<label class='item item-input'>
<span class='input-label'>Start Date</span>
<input type='date' ng-model='userSale.startDate'>
</label>
<!-- More data fields -->
但是,当我更改字段中的数据时,似乎什么也没发生。我知道正在创建和修改一个对象,但它不会将更改发布到 Firebase。我也尝试过使用 .$save() 操作,但这似乎没有什么不同。我也尝试过使用这个命令手动设置数据:
UserSale($scope.authData.uid).title = $scope.userSale.title;
但最终什么都没有写入数据库。
这是我的 Firebase 安全规则:
{
"rules": {
// Public read access
".read": true,
// Users write to their own key
"users": {
"$uid": {
".write": "auth != null && auth.uid == $uid"
}
}
}
}
我通过模拟器对它们进行了测试,一切正常。我也尝试在没有安全规则的情况下运行它,但这似乎也没有任何作用。
感谢您的帮助! :)
【问题讨论】:
-
不需要初始化数据。您的代码看起来正确。您的安全规则是什么样的?
-
更新了我的安全规则!
-
您的控制台有错误吗?您确定用户已通过身份验证吗?我们可能需要查看您的控制器代码。
-
我每次运行应用程序时都会收到这些errors,但据我所知,它们并没有影响任何东西。我几乎可以肯定用户已通过身份验证,因为输入字段仅在存在身份验证数据时显示。我编辑了我的帖子,添加了更多我的控制器和工厂。
标签: html angularjs data-binding ionic-framework firebase