简体   繁体   English

从下至上将所有表格重新设置为原始状态

[英]Re-set all forms to pristine status from the bottom up

We have smDateTimePicker directive which is rather complex. 我们有一个非常复杂的smDateTimePicker指令。 This time picker control can be part of another directive and that directive can be placed into the form (or a tab). 此时间选择器控件可以是另一个指令的一部分,并且该指令可以放入表单(或选项卡)中。 In that smDateTimePicker directive we have code resetting form to pristine status (and I added last 2 lines just now): 在该smDateTimePicker指令中,我们将代码重置形式设置为原始状态(我刚才添加了最后两行):

 DateTimePicker.prototype.setPristine = function () { if (this.form) { this.form.$setPristine(); if (this.form.$$parentForm.$dirty) { this.form.$$parentForm.$setPristine(); } if (this.form.$$parentForm.$$parentForm && this.form.$$parentForm.$$parentForm.$dirty) { this.form.$$parentForm.$$parentForm.$setPristine(); } } }; 

I don't like that I need to use $$parentForm property and I need to use it twice for my particular case. 我不喜欢我需要使用$$ parentForm属性,并且在我的特定情况下需要使用两次。 Besides, what if I have deeper hierarchy? 此外,如果我有更深的等级怎么办? Is there a cleaner way to re-set all forms to pristine from the bottom up? 是否有一种更干净的方法将所有表格从下至上重新设置为原始格式?

See if this works for you. 看看这是否适合您。 You may have to do minor changes if needed: 如果需要,您可能需要做一些小的更改:

DateTimePicker.prototype.setPristine = function () {
        if (this.form) {
            setPristineToForm(this.form);
        }
    };

    // Recursive method - goes up the hierarchy of forms
    var setPristineToForm = function (form) {
        if (form) {
            //Check if a parent form exists. If it does, set parent's pristine 
            var parentForm = this.form.$$parentForm;

            if (parentForm) {
                setPristineToForm(parentForm);
            }
            else { //No parent exist. Set pristine to current form if it is dirty.
                if (form.$dirty)
                    form.$setPristine();
            }
        }
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM