Angularjs - 在Master Detail Scenario中对未保存的更改发出警告

时间:2017-01-01 02:41:39

标签: javascript angularjs html5 savechanges

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

Sam提供的解决方案非常适合html文件中的单个表单,但我遇到的问题是我在标题部分和详细信息部分中有多个表单可以更改。在细节部分我使用xeditable表。我试过添加"确认退出"属于两种形式。在顶部,确认框按预期出现,但即使我提交更新后仍然会出现。在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

1 个答案:

答案 0 :(得分:1)

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

到目前为止工作得非常好。