【发布时间】:2015-08-17 14:04:24
【问题描述】:
在范围对象 userNotify 上有一些继承问题(希望如此)。
AccountCtrl 处理用户帐户数据的查看和更新,数据通过 UserService 服务来自 Parse.com db。
.controller('AccountCtrl', [
'$state', '$scope', 'UserService',
function ($state, $scope, UserService) {
UserService.currentUser().then(function (_user) {
$scope.user = _user;
notification = _user.get('days');
$scope.userNotify = {days: notification};
});
$scope.updateUser = function (_user) {
days = $scope.userNotify.days;
// days is logged with the correct value (if the user changed)
// but the db record is updated with the initial value not the new one.
// tested this by changing days to a random number.
console.log(days);
_user.set('days', days);
_user.save();
}
}])
数据显示在视图中,updateUser 函数应该更新 Parse db 中的用户数据。
<div ng-controller="AccountCtrl">
<select ng-model="$parent.userNotify.days">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<a ng-click="updateUser($parent.user)">Save</a>
</div>
当 _user.set 在 updateUser 函数中被调用时,$scope.userNotify.days 具有来自数据库的旧值,因此使用相同的值更新,而不是用户选择的新值。 即使 console.log(days) 显示正确的新值。
还尝试删除 $parent 并使用原始类型。
这里是 UserService,它返回一个 Promise:
.service('UserService', ['$q', 'ParseConfiguration',
function ($q, ParseConfiguration) {
return {
/**
* @param _parseInitUser
* @returns {Promise}
*/
currentUser: function (_parseInitUser) {
_parseInitUser = Parse.User.current();
if (!_parseInitUser) {
return $q.reject({error: "noUser"});
} else {
return $q.when(_parseInitUser);
}
}
}
}]);
非常感谢
【问题讨论】:
-
是否将正确的日期保存到数据库中?
-
它可能不相关,但您应该使用
var语句来避免在全局范围内创建变量。喜欢var notification = ...和var days = ...; -
我确实使用了
var,但没有任何效果。如果我使用随机数,它会保存到数据库_user.set('days', 15);这意味着数据库集正在工作,但该值并未从updateUser函数的视图中更新。 -
是否也在考虑将 ng-change 与另一个模型或 $watch 一起使用,这里是否有必要这样做?
标签: javascript angularjs parse-platform