Javascript 使用Angular.js移动到另一个页面时,表单内部的指令不起作用

Javascript 使用Angular.js移动到另一个页面时,表单内部的指令不起作用,javascript,angularjs,forms,angularjs-directive,Javascript,Angularjs,Forms,Angularjs Directive,我这里有一个问题。我的应用程序中有一个表单,我的要求是当用户导航到另一个页面而不提交此表单时,将显示一条确认消息,以使用angular.js提醒用户。我在下面解释我的代码 <form name="billdata" id="billdata" confirm-on-exit enctype="multipart/form-data" novalidate> <span class="input-group-addon ndrftextwidth text-right"

我这里有一个问题。我的应用程序中有一个表单,我的要求是当用户导航到另一个页面而不提交此表单时,将显示一条确认消息,以使用angular.js提醒用户。我在下面解释我的代码

<form name="billdata" id="billdata" confirm-on-exit enctype="multipart/form-data" novalidate>
    <span class="input-group-addon ndrftextwidth text-right" style="width: 180px;">First Name :</span>
    <input type="text" name="itemname" id="contactno" class="form-control" placeholder="user Name" ng-model="first_name">
    <span class="input-group-addon ndrftextwidth text-right" style="width: 180px;">Last Name :</span>
    <input type="text" name="itemname" id="contactno" class="form-control" placeholder="user Name" ng-model="last_name">
    <div style="text-align: center; padding-top: 10px;">
        <input type="button" class="btn btn-success" ng-click="addUserData(billdata);" id="addProfileData" value="submit" />
    </div>
</form>

在这里,当用户完全离开页面站点时,我收到确认消息。但我的要求是,当用户导航到另一个页面而不提交此表单时,应显示确认消息。请帮助我。

确定,因此我创建了一个包含两条路线的小项目。为了演示如何做你想做的事。。。这是您的代码片段,因此只需从中获取您需要的内容

该指令已更改为:

.directive('confirmOnExit', function() {
return {
  require:'form',
        link: function($scope, elem, attrs,formController) {
            window.onbeforeunload = function(){
                if (formController.$dirty) {
                    return "The form is not saved, do you want to stay on the page?";
                }
            }
               $scope.$on('$locationChangeStart', function (event, next, current) {
            if (formController.$dirty) {
                if (!confirm("The form is dirty, do you want to stay on the page?")) {
                    event.preventDefault();
                }
            }
        });
          }
    };
}))

并不是说我添加了require表单,使其成为您想要使用的所有表单的通用形式

<>这只会考虑$肮脏,所以如果你也想$$无效,添加条件和提交的$/P/>
**这里有一个**和工作示例…

请查看此详细说明,了解您要查找的内容@J-D:我以前也访问过这个网站,不清楚这一点。如果你不想在表单是原始的情况下移出页面,那么你还需要在if之外设置prevent默认值,因为在这里,当表单不是脏的时候,我们不会进入这种状态,然后允许位置继续。。。尝试将event.preventDefault()添加到if之外,另外$dirty并不意味着submited如果您不关心有效性,则存在$submit属性,如果您do@Jony-Y:你能编辑一下你的答案吗?当然可以,创建一个plunker,我会帮助@Jony-Y:看起来不错。你能告诉我这里的
formController
是什么吗?当然,当你将require添加到指令中时,在这种情况下,“form”实际上会将表单传递到指令中,允许你访问它。。。因此,您可以使用表单控制器函数,如$dirty或$setDirty()等,如果您想查看更多详细信息,您可以将require用于多种用途,以及require部分@Jony-Y:无论如何,谢谢。它按照要求工作。
.directive('confirmOnExit', function() {
return {
  require:'form',
        link: function($scope, elem, attrs,formController) {
            window.onbeforeunload = function(){
                if (formController.$dirty) {
                    return "The form is not saved, do you want to stay on the page?";
                }
            }
               $scope.$on('$locationChangeStart', function (event, next, current) {
            if (formController.$dirty) {
                if (!confirm("The form is dirty, do you want to stay on the page?")) {
                    event.preventDefault();
                }
            }
        });
          }
    };