【问题标题】:Angular data binding not updating correct model scope角度数据绑定未更新正确的模型范围
【发布时间】: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.setupdateUser 函数中被调用时,$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


【解决方案1】:

试试这个作为 html 中的 sn-p

<body ng-controller="MainCtrl as main">
  <ion-view>
    <label class="item item-input item-select">
      {{currentSelection.value}}
      <select ng-model="currentSelection" ng-options="day as day.value for day in days track by day.value" style="width: 30px">
      </select>
    </label>
    <a class="button" ng-click="updateUser($parent.user)">Save</a>
  </ion-view>
</body>

这是javascript代码

angular.module('app', ['ionic'])
  .controller('MainCtrl', ['$scope', function($scope) {

    $scope.currentSelection = {};
    $scope.currentSelection.value = 1;

    $scope.days = [
      { value: 1 }, 
      { value: 2 }, 
      { value: 3 }
    ];
    $scope.updateUser = function() {
      days = $scope.currentSelection;
      alert(JSON.stringify(days));
    }
  }]);

不确定您为什么要使用$parent 来访问用户?当前只有一个用户,Parse 提供了一项功能,让您可以在整个应用程序中访问它。

$scope.updateUser = function() {
  UserService.currentUser().then(function (_user) {
    return _user.save({days:daysValue});
  }).then(function(_updatedUser){
    console.log(_updatedUser);
  }, function(_error) {
    console.log(_error);
  });
};

【讨论】:

  • 这仍然从 Parse 返回初始值,即使它在我更改选择时更新视图。我正在使用$scope.currentSelection.value = _user.get('days'); 设置初始选择值
  • 感谢您的帮助,我得到了它的工作,请参阅下面的答案。
  • 这里根本没有足够的代码来理解您的问题......正如我所说,您根本不需要父范围,请参阅上面的编辑
【解决方案2】:

我通过将视图中的天数传递给 updateUser 函数来让它工作。

在视图中:

&lt;a ng-click="updateUser(user,userNotify.days)"&gt;Save&lt;/a&gt;

在控制器中:

$scope.updateUser = function (_user,days) { console.log(days); }

【讨论】:

    猜你喜欢
    • 2018-06-10
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 2014-11-16
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多