Examples of the use of $watch and $timeout in Angularjs _angularjs

Source: Internet
Author: User


Objective



It is believed that the use of angular partners should be$watchfamiliar with this monitoring, it is mainly used to monitor the changes in the model, when your model part of the change it will notify you. In the recent platform management development, I also use this angular, in the filter can not avoid the use of$watchmonitoring. At that time my idea is to search the time does not need to click the Search button, so in the user experience is also excellent, to avoid the input and then click the operation steps.



And then, when it comes to listening, the$watchfirst code is this.


$scope. $watch (' Filteroptions ', function (newval, oldval) {
 if (newval!== oldval) {
 //filteroptions when changes occur, call method c7/> $scope. Getpageddataasync (' admin/bill/' + $stateParams. page+ ' pagecount= ' + $scope. Paginationconf.itemsperpage, $scope. filteroptions);
 }
, True);


In doing so, it doesn't look like a big problem, it pulls the data every time you enter it. But! But! But! The important thing to say three times! When the user input "John" This search word, the code is this way, "Zhang" word pull a data, "three" word has pulled a data. Well, this is still two words, if the input of 10 words, the server will cry, the server to hit the front.



This time you need to use the goods in the angular--$timeoutHe is a timer in the angular. For our search, when listening tofilterOptionschange, do not immediately request, give him a 0.8 seconds of a wait time, if this 0.8 secondsfilterOptionshave not changed again, then request pull data, or continue to enter.



The code is as follows:


$scope. $watch (' Filteroptions ', function (newval, oldval) {
 if (newval!== oldval) {
  if (timeout) $timeout. Cancel (timeout);
  Timeout = $timeout (function () {
  $scope. Getpageddataasync (' admin/bill/' + $stateParams. page+ '? pagecount= ' +$ Scope.paginationConf.itemsPerPage, $scope. filteroptions);
  },
 true);


Summarize



The above is the entire content of this article, I hope to be able to learn or work to bring certain help, if there is doubt you can message exchange.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.