亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

angular.js - $watch monitors the drop-down box. It will be automatically executed once when the web page is refreshed. How to disable automatic execution?
漂亮男人
漂亮男人 2017-05-15 17:04:22
0
3
1014

Detailed description:
$watch monitors a drop-down box, and the expectation is that only when the user clicks an option in the drop-down box, the front end will request the back end to update data.

The current problem is:
When the web page is loaded and the drop-down box appears for the first time, $watch will be automatically executed once. The ng mechanism believes that loading is a user click.

Want to know how to disable automatic execution of $watch in this case

漂亮男人
漂亮男人

reply all(3)
迷茫

Thanks for the invitation.

First of all, can I correct a small flaw in wording.

You said "$watch monitors a drop-down box", ng是一個實現(xiàn)數(shù)據(jù)雙向綁定的,具有聲明式API的框架,這里的$watch是不可能去監(jiān)視一個DOM元素下拉框that's right. The appropriate wording can be written as "an expression monitored by $watch that points to the selected value of the drop-down box" which is more accurate (friends who are good at Chinese can further optimize).

When asking questions, we tend to focus too much on the answers we want to know and ignore the way of expression. As a result, we often end up in vain. This type of question is very common in SF. The questioner’s description is vague, making it impossible to answer. In fact, I can still understand the description of the question, but I just want to say a few more words because of my feelings.

Back to the subject, since you don’t want to process the data initialization time$watch, you can judge it like this

$scope.$watch('表達(dá)式', function(newVal, oldVal){
    //當(dāng)之前值和當(dāng)前值相同,或者之前值為null或undefined時
    if(newVal === oldVal || oldVal == null){
        //不執(zhí)行任何操作
        return;
    }
    
    //你的正常代碼,寫這里
});
PHPzhong
http://docs.ngnice.com/api/ng/type/$rootScope.Scope

scope.$watch('name', function(newValue, oldValue) {
  if(newValue == oldValue) return;
  scope.counter = scope.counter + 1;
});
習(xí)慣沉默

Use ngChange to avoid initialization calls

Attention:

  • Needs to be used together with ngModel

  • Using a program to change the value of ngModel will not trigger ngChange

Evaluate the given expression when the user changes the input. The expression is evaluated immediately, unlike the JavaScript onchange event which only triggers at the end of a change (usually, when the user leaves the form element or presses the return key).

ngChange Documentation

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template