简体   繁体   English

AngularJs列表控件-如何避免设置表单的脏标志

[英]AngularJs list control - how to avoid setting form's dirty flag

We have a mover directive that uses 2 list controls. 我们有一个使用2个列表控件的mover指令。 The problem is that when I simply click on an item in the assigned items list, it makes my form dirty. 问题是,当我只单击分配的项目列表中的一个项目时,它会使我的表单变脏。 I tried the following solution in the directive controller onChanged event (which is called by the second list ng-change): 我在指令控制器onChanged事件(由第二个列表ng-change调用)中尝试了以下解决方案:

  $scope.onChanged = function (assigned) { var currentState = $scope.form.$dirty; $scope.selectedItem = assigned[0]; if (currentState === false) $scope.form.$setPristine(); } 

However, the currentState is already true, so my code does nothing. 但是,currentState已经为真,因此我的代码不执行任何操作。 How can I prevent the clicking in the list control to set form's dirty status? 如何防止单击列表控件来设置表单的脏状态? I found two related questions How can I exclude controls from making form Dirty? 我发现了两个相关的问题, 如何将控件排除在制作“肮脏”表单之外? and How can I denote which input fields have changed in AngularJS but it's not clear to me if I should try either of these solutions. 以及如何指示AngularJS中哪些输入字段已更改,但是我不清楚我是否应该尝试这两种解决方案。 The code checks for $scope.form.$dirty in a few places, so the best solution is somehow to make sure that clicking on the list doesn't make it dirty. 该代码在一些地方检查$ scope.form。$ dirty,因此最好的解决方案是以某种方式确保单击列表不会使它变脏。 I also have noDirty directive which I haven't yet tried applying to that list. 我也有noDirty指令,尚未尝试将其应用于该列表。 I'm going to try that now. 我现在要尝试。

The solution I implemented for now is the following change in the directive: 我现在实现的解决方案是指令中的以下更改:

 var directive = { controller: ['$scope', '$timeout', function ($scope, $timeout) { $scope.upDisabled = true; $scope.downDisabled = true; $scope.assigned = null; $timeout(function () { $scope.form.assignedList.$pristine = false; }); 

based on last answer in that thread Prevent input from setting form $dirty angularjs . 基于该线程的最后一个答案阻止输入设置形式$ dirty angularjs In my quick limited test it seems to work. 在我的快速有限测试中,它似乎有效。 I originally tried to just use our noDirtyCheck directive on the list, but somehow it didn't work in my first try, so I already switched to this solution. 我最初尝试仅在列表中使用我们的noDirtyCheck指令,但是由于某种原因,它在我的第一次尝试中不起作用,因此我已经切换到该解决方案。

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

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