javascript - Angularjs – 对主细节场景中未保存的更改发出警告

标签 javascript angularjs html savechanges

我需要一个解决方案来警告主从场景中未保存的更改。我能够实现 Sam 在 2015 年 9 月创建的解决方案 ( http://www.samjhill.com/blog/?p=525#comment-28558 )。这是对链接 Detect Unsaved changes and alert user using angularjs 上的 stackoverflow 问题和答案的回应。 .

Sam 提供的解决方案非常适合 html 文件中的单个表单,但在标题部分和详细信息部分中有多个表单(两者都可以更改)的情况下,我遇到了问题。在详细信息部分,我使用 xeditable 表。我尝试将“退出时确认”属性添加到这两种表单中。在顶部,确认框按预期出现,但即使在我提交更新后仍继续出现。在可编辑部分,确认框永远不会出现。我已经在下面包含了我的 html。您能否提供我可以提出的任何建议,以便在对页面上的任何表单进行更改时出现警告?

.container.col-md-12.col-sm-12.col-xs-12-center
    .well
        form.form-horizontal(name="createQuestionAnswer" confirm-on-exit)
            fieldset
                legend Question Definition
                .form-group
                    label.col-md-2.control-label(for="statement") Question
                    .col-md-10
                        input.form-control(name="statement", type="text", placeholder="Statement", ng-model="statement", ng-change = "changeStatement()",required)
                .form-group
                    label.col-md-2.control-label(for="sequenceNo") SequenceNo
                    .col-md-4
                        input.form-control(name="sequenceNo", type="text", placeholder="SequenceNo", ng-model="sequenceNo", ng-change = "changeSequenceNo()",required)
                    label.col-md-2.control-label(for="componentType") Type
                    .col-md-4
                        input.form-control(name="componentType", type="text", placeholder="ComponentType", ng-model="componentType", ng-change = "changeComponentType()",required)
                .form-group
                    label.col-md-2.control-label(for="weight") Weight
                    .col-md-2
                        input.form-control(name="weight", type="text", placeholder="Weight", ng-model="weight", ng-change = "changeWeight()")
                    label.col-md-2.control-label(for="imageURL") Image
                    .col-md-6
                        input.form-control(name="imageURL", type="text", placeholder="Image", ng-model="imageURL", ng-change = "changeImageURL()", required)
    div(ng-app='app')
            table.table.table-bordered.table-hover.table-condensed(style='width: 100%')
                tr(style='font-weight: bold')
                    td(style='width:70%') Answer
                    td(style='width:10%') Correct
                    td(style='width:10%') Rank
                    td(style='width:10%') Position
                tr(ng-repeat='answer in answers')
                    td
                        // editable answer (text with validation)
                        span(editable-text='answer.answer',  e-name='answer', e-form='rowform', onbeforesave='checkAnswer($data)',onaftersave='saveAnswers($data)', e-required='') {{ answer.answer || 'empty'}}
                    td
                        // editable  (select-local)
                        span(editable-text='answer.correct', e-name='correct', e-form='rowform') {{ answer.correct }}
                    td
                        // editable
                        span(editable-text='answer.rank', e-name='rank', e-form='rowform' ) {{ answer.rank }}
                    td
                        // editable
                        span(editable-text='answer.position', e-name='position', e-form='rowform') {{ answer.position }}
                    td(style='white-space: nowrap')
                        // form
                        form.form-buttons.form-inline(editable-form='', name='rowform' onbeforesave='changeAnswers()', ng-show='rowform.$visible', shown='inserted == answer')
                            button.btn.btn-primary(type='submit', ng-disabled='rowform.$waiting') save
                            button.btn.btn-default(type='button', ng-disabled='rowform.$waiting', ng-click='rowform.$cancel()') cancel
                        .buttons(ng-show='!rowform.$visible')
                            button.btn.btn-primary(ng-click='rowform.$show()' ) edit
                            button.btn.btn-danger(ng-click='removeAnswer($index)') del
            button.btn.btn-default(ng-click='addAnswer()') Add row
form.form-horizontal(name="buttonQuestionAnswer")
    fieldset
        .form-group
            .col-md-10.col-md-offset-2
                .pull-right
                    button.btn.btn-primary(ng-controller="flQuestionCrudCtrl", ng-show="questionMode=='Create'",  ng-click="createQuestion()") Save New
                    |  
                    button.btn.btn-primary(ng-controller="flQuestionCrudCtrl", ng-show="questionMode=='Update'", ng-click="updateQuestion()") Update
                    |  
                    button.btn.btn-primary(ng-controller="flQuestionCrudCtrl", ng-show="questionMode=='Update'", ng-click="deleteQuestion()") Delete
                    |  
                    a.btn.btn-default(ui-sref="home") Cancel

最佳答案

因为我不知道 HTML 存在嵌套表单元素的问题,所以这是一个很好的提示。我删除了按钮表单,并将“创建/更新/删除”按钮作为标题表单的一部分。然后,我将表单名称传递给函数来创建、更新和删除记录。在函数中我添加了代码: myForm.$setPristine(true); myForm.$setSubscribed(true); myForm.$setDirty(false); 在 xeditable 表单中,我还将主表单的名称传递给 onbeforesave='changeAnswers(headerForm)'。在changeAnswers函数中我添加了代码: myForm.$setDirty(true);

到目前为止,工作进展顺利。

关于javascript - Angularjs – 对主细节场景中未保存的更改发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41413015/

相关文章:

javascript - 在 Xamarin 中从 C# 对象调用 JavaScript

javascript - @Ngrx/存储 : how to query models with relationships

javascript - d3.js 圆弧段动画问题

javascript - Sequelize Reuse Query 在另一个查询中

javascript - Angular 在单击时隐藏 ng-repeat 中的元素

jquery - 在 vue 中使用 css/scss 应用动态样式

javascript - 制作新圆标记时删除圆标记

javascript - 如何通过单击另一个按钮使按钮可见?

javascript - 如何从浏览器编辑 JS?

javascript - Angularjs $document[0].activeElement 而不是 $document.activeElement