如何销毁angularjs指令?

问题描述

我在SPA应用程序的表单中插入了AngularJS指令。它的作用是在转换到新状态之前检查表单是否脏了。这是下面的代码。

app.directive('leavepage',['$transitions','$uibModal','$q',function ($transitions,$modal,$q) {
        return {
            restrict: 'A',require: '^form',link: function ($scope,$elem,$attr,form) {
                $transitions.onBefore({},function (transition) {

                    if (form.$dirty) {
                        return $q.when($modal.open({
                            backdrop: 'static',keyboard: false,controller: 'modalController',template: "<div class='modal-header'><h5 class='modal-title'>Leave page?</h5></div>"
                                + "<div class='modal-body'><p>Changes you made may not be saved.</p></div>"
                                + "<div class='modal-footer'><button type='button' class='btn btn-light' ng-click='close(true)'>Cancel</button>"
                                + "<button type='button' class='btn btn-outline-success' ng-click='close(false)'>Leave</button></div>"
                        }).result.then(function (cancel) {
                            if (cancel) {
                                return false;
                            }
                        }));
                    }
                });
            }
        };
    }
]);

但是,如果表单脏了,并且用户加载了另一个表单,则form。$ dirty始终为true,并在每次其他表单弄脏时相乘。然后,您确定要离开模板出现多次。在转换完成后,如何获取此指令以将其自身删除,然后以新形式重新插入自身?

谢谢,感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)