检测使用angularjs未保存的数据 [英] Detect unsaved data using angularjs

查看:209
本文介绍了检测使用angularjs未保存的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手到AngularJs,所以这可能是微不足道的。
是否有任何内置的AngularJs 指令的形式来检测未保存的数据。
如果没有,那么如何去写的。
任何指针将AP preciated。

I'm a newbie to AngularJs, so this might be trivial. Are there any inbuilt AngularJs directive to detect unsaved data in a form. If not then how to go about writing one. Any pointers would be appreciated.

HTML code是

html code is

<input type="text" runat="server" />

和我的角度JS控制器code是

And my angular js controller code is

    function MyCtrl1($scope) {
      // code to do stuff
}MyCtrl1.$inject = ['$scope'];

我试图写一个指令检测未保存的数据,而且我猜测它在上controller.Correct我,如果错了。

I am trying to write a directive to detect unsaved data, and I'm guessing its to be written in the above controller.Correct me if wrong.

推荐答案

AngularJS设置CSS类 NG-质朴 NG-脏上的任何输入字段您已经使用NG-模型上,和你的FormController拥有的属性 $质朴 $脏,你可以查看是否窗体是脏与否。所以,是的,这是可能的。

AngularJS sets the CSS classes ng-pristine and ng-dirty on any input field you've used ng-model on, and your FormController has the properties $pristine and $dirty which you can check to see if the form is dirty or not. So yes, it's possible.

您能提供一些code,显示你想要做什么?这将使它更容易帮助你。

Could you provide some code that shows what you're trying to do? That would make it easier to help you.

修改

下面是如何检测的原始/肮脏的状态,以及如何恢复到原始状态一个简单的例子:

Here's a simple example of how to detect a pristine/dirty state, and how to revert to a pristine state:

<!doctype html>
<html ng-app>
<head>
    <script src="http://code.angularjs.org/1.1.2/angular.min.js"></script>
    <script type="text/javascript">
    function Ctrl($scope) {
        var initial = {text: 'initial value'};
        $scope.myModel = angular.copy(initial);
        $scope.revert = function() {
            $scope.myModel = angular.copy(initial);
            $scope.myForm.$setPristine();
        }
    }
    </script>
</head>
<body>
    <form name="myForm" ng-controller="Ctrl">
        myModel.text: <input name="input" ng-model="myModel.text">
        <p>myModel.text = {{myModel.text}}</p>
        <p>$pristine = {{myForm.$pristine}}</p>
        <p>$dirty = {{myForm.$dirty}}</p>
        <button ng-click="revert()">Set pristine</button>
    </form>
</body>
</html>

这篇关于检测使用angularjs未保存的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆