简体   繁体   English

当JavaScript更新输入值时,AngularJS过滤不起作用

[英]When JavaScript updates an input value, AngularJS filtering doesn't work

I'm trying to do some stuffs with Angular from a few days and I'm stucked with an update problem. 几天以来,我一直在尝试使用Angular做一些事情,但遇到了更新问题。

This is part of my HTML page : 这是我的HTML页面的一部分:

<div ng-app="monApp" ng-controller="monControleur">
Date de début : <input type="text" ng-model="date_deb" placeholder="JJ/MM/AAAA" value="" name="date_d" id="champ_date_deb" size="12" maxlength="10">&nbsp;<span id="calendarMainDeb"></span>
<script type="text/javascript">
//<![CDATA[
    calInit("calendarMainDeb", "Calendrier Deb", "champ_date_deb", "jsCalendar", "day", "selectedDay","calendarWrap1");
//]]>
</script>
<table border='1'>
<tr data-ng-repeat="evenement in mydata | entre_deux_dates:date_deb:date_fin ">
....

When I input a new date_deb with the keyboard, displayed values are filtered with Angular. 当我使用键盘输入新的date_deb时,显示的值将使用Angular过滤。 On the contrary, if a use a JS calendar to set the date_deb value, it sets the value but no filtering is realized unless giving focus in JS and pressing "space". 相反, 如果使用JS日历设置date_deb值,则它会设置该值,但是除非实现JS的焦点并按“空格”,否则不会实现任何过滤。

Here is the last part of the JS which updates the date value : 这是JS的更新日期值的最后一部分:

field.value = dateArr[0]+'/'+dateArr[1]+'/'+dateArr[2];
field.focus();

What can I do to dynamically update the filtered values right after a date is picked in the calendar ? 在日历中选择日期之后,我该怎么做才能动态更新过滤后的值? I wouldn't like the user to have to press "space" after each selection ... 我不希望用户在每次选择后都必须按“空格”。

I have tried a lot of solutions in angular (ng-model-options, etc.) and in JS but none worked. 我在角度(ng-model-options等)和JS中尝试了很多解决方案,但都没有用。

You need to invoke a digest cycle: 您需要调用摘要循环:

 $scope.$applyAsync(function() {
         field.value = dateArr[0]+'/'+dateArr[1]+'/'+dateArr[2]; 
         field.focus();
});

That is because angular works by dirty checking - in each digest cycle (apply means run a digest for the root scope), all watchers in the scope hierarchy are checked for changes, and updates occur as necessary. 这是因为角度检查是通过脏检查来进行的-在每个摘要周期中(应用意味着对根范围进行摘要),将检查范围层次结构中的所有观察程序的更改,并根据需要进行更新。

You can put your answer in $timeout, as $timeout runs the digest cycle as well as it saves from the error "$apply alreay running". 您可以将答案放入$ timeout中,因为$ timeout会运行摘要循环,并且可以避免错误“ $ apply alreay running”。

$timeout(function() {
     field.value = dateArr[0]+'/'+dateArr[1]+'/'+dateArr[2]; 
  });

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

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