简体   繁体   中英

Which gets executed first? onclick or ng-click

Now, I have a button and I have a validation process taking place in its click [onclick]. I have a web service call and other processes if the validation is successful, which I have in the ng-click. I doubt validation is not taking place.

My Page:

<script>
    validate(){

    // my validation code goes here...

}
</script>

<input type="button" id="btnLogin" title="Login" value="Login" onclick="validate();" ng-click="login_authentication();"/>    

My Controller:

$scope.login_authentication = function() {

 // My login code goes here....

        }

Ideally you shouldn't be using onclick at all in angular if you can avoid it. Run validate() in your ng-click and then run login_authentication() if successful. Alternatively use angular's ng-form style validation for your inputs.

You should avoid using onclick in angularjs.

For answer of your question onClick will be called first I tried this code which tells that that onClick will be called first.

<div ng-controller="ControllerZero">
    <button ng-click="angularNgClick()" onclick="jsClick()">Click</button>
</div>

function ControllerZero($scope) {   
     $scope.angularNgClick = function() {
        alert('Angular NG Click');
    };
}

var jsClick = function() {
        alert('js on Click');
    };

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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