angular-strap datepicker updateOn提交

时间:2014-12-09 10:49:20

标签: angularjs angular-ngmodel angular-strap

http://plnkr.co/edit/I5JkHiNnwmklHAKh3ag9?p=preview

<form ng-model-options="{ updateOn: 'submit' }">
<input type="text" class="form-control" ng-model="activeEditorData.startDate" data-date-format="yyyy-MM-dd" data-date-type="number" data-min-date="02/10/86" data-max-date="today" data-autoclose="1" name="date2" bs-datepicker>
<button type="button" class="btn btn-default" ng-click="$hide()">Cancel</button>
<button type="submit" class="btn btn-primary" ng-click="$hide()">Save changes</button>
</form>

我遇到了ng-model-options和angular-strap datepicker的问题。按下save时,实际的$ scope.date完全按预期更新,但是datepicker输入日期不会更改,因为它在更改日期时应该更新。有没有办法强制日期选择器更新它的值或什么?

如果删除ng-model-options属性,它会按预期工作,但这样就无法在取消按下时回滚所有数据,这对于更大的表单是有问题的。

1 个答案:

答案 0 :(得分:0)

因此,某些指令似乎不喜欢当前父作用域之外的双向绑定,因此我只是删除了ng-model-options并制作了该对象的副本。

控制器:

    /**
     * ng-model-options won't work with certain directives,
     * so this is needed to replicate its behaviour.
     */
    $scope.dataCopy = angular.copy($scope.activeEditorData);

    $scope.save = function(hide) {
        angular.forEach($scope.dataCopy, function(value, key) {
            $scope.activeEditorData[key] = value;
        });
        $scope.exit(hide);
    };

HTML:

<form>
    <input type="text" class="form-control" ng-model="dataCopy.startDate" data-date-format="yyyy-MM-dd" data-date-type="number" data-min-date="02/10/86" data-max-date="today" data-autoclose="1" name="date2" bs-datepicker>
    <button type="submit" class="btn btn-primary" ng-click="save($hide)">Save changes</button>
</form>