Prevent input from setting form $dirty angularjs

前端 未结 7 1434
天涯浪人
天涯浪人 2021-02-07 05:37

I have an ng form on a page. Inside the form I have several controls which need to display a save dialog when the form is dirty, ie form.$dirty = true. However there are some na

7条回答
  •  醉梦人生
    2021-02-07 06:33

    Setting the $pristine property to false, only when initializing, works until you call $setPristine() on the form. Then your control has its $pristine back to true and changing the input's value would make your form dirty. To avoid that, set the $pristine on focus:

    link: function(scope, elm, attrs, ctrl) {
        elm.focus(function () {
            ctrl.$pristine = false;
        });
    }
    

提交回复
热议问题