【问题标题】:Modify form field value when updating a different field in a ng-admin form更新 ng-admin 表单中的不同字段时修改表单字段值
【发布时间】:2016-05-02 14:40:02
【问题描述】:

每当我启用不同的复选框字段(将其设置为 true)时,我都需要在实体的 EDIT 视图中更新“日期时间”字段值。

NGA 字段

nga.field('launchDate', 'datetime')
       .label('Launch date')

nga.field('active', 'boolean')
       .template('<active-game-field field="::field" entity="::entity" game="entry"></active-game-field>')

问题是,在我为“活动”模板创建的指令中,我必须执行一些逻辑,然后我想从那里修改“launchDate”的前端元素值。

有没有一种方法可以轻松地做到这一点,例如使用像'::fields'这样的属性,将表单的所有字段传递给指令?

现在我可以通过 require: '^form' 或 '::entity' 访问这些字段,但这些值仅针对 JSON 请求/响应进行修改,而不是前端字段上的值。

指令

module.exports = ['ngDialog', function activeGameField(ngDialog) {
return {
    require: '^form',
    restrict: 'E',
    scope: {
        'field': '&',
        'game': '&',
        'entity': '&'
    },
    link: function (scope, element, attrs, formCtrl) {

        scope.updateLaunchDate = function (elem) {
            document.activeElement.blur();

            if (elem.value == true) {
                ngDialog.openConfirm({
                    template: '\
                    <p>Do you want to update the launch date of this game to now?</p>\
                    <div class="ngdialog-buttons">\
                        <button type="button" class="ngdialog-button ngdialog-button-secondary" ng-click="confirm(false)">No</button>\
                        <button type="button" class="ngdialog-button ngdialog-button-primary" ng-click="confirm(true)">Yes</button>\
                    </div>',
                    showClose: false,
                    closeByDocument: false
                }).then(function (value) {
                    if (value) {
                        var date = new Date(Date.now());
                        scope.game().values.launchDate = date; // JSON value modified properly after clicking on save entity, but not the frontend value. In this callback I would like to do something like\'elem.value = date;' but for the 'launchDate' element instead if 'this' (active checkbox element).                   
                    }
                });
            }
        }
    },
    template:
        '<input type="checkbox" ng-model="value" id="active" name="active" class="form-control ng-pristine ng-untouched ng-valid" \
            ng-click="updateLaunchDate(this)">'
};

提前致谢。

【问题讨论】:

    标签: ng-admin


    【解决方案1】:

    我不太喜欢,但最后我通过 JavaScrit 通过 ID 访问“launchDate”元素并修改元素值直接解决了这个问题。

    document.getElementById("launchDate").value = dateFormat(now, "yyyy-mm-dd HH:MM:ss");
    

    我更希望通过 angular 或 ng-admin 来做这件事,但我无法用这种方法弄清楚。

    【讨论】:

      猜你喜欢
      • 2014-10-03
      • 2012-02-25
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      相关资源
      最近更新 更多