Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ng-click not work in case-1, but does in case-3?

Tags:

angularjs

There is clearly something fundamental im not yet understanding.

Im trying to make user of the Modal module in Angular Ui.Bootstrap but im finding that my clicks are not activating the open() function -- So boiling it down to a very simple testcase, as below, im not seeing any calls when the ng-click points to a function (alert or console.log), but does work when the ng-click points to something which is just an expression

Why is the alert not called in the first example?

<div data-ng-app>     <button data-ng-click="alert('Message 1');">         ngClick -- not working, why not?     </button>     <button onclick="alert('Message 2');">         Plain onclick     </button>     <button data-ng-click="count = (count + 1)">           But this works, why ???     </button>     count: {{count}} </div> 

http://jsfiddle.net/H2wft/1/

like image 914
Soren Avatar asked May 18 '14 18:05

Soren


People also ask

Can we call two functions on Ng-click?

When an HTML is clicked, the ng-click directive tells the AngularJS script what to do. In this article, we will learn how to get many/multiple functions to the ng-click directive passed, in just one click. The key is to add a semi-colon (;) or a comma (,). All the functions must be separated by a (;) or a (, ).

What is the difference between click and Ng-click?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.


2 Answers

ng-click is meant for use with either a function in the current scope (so for example $scope.alert = window.alert would solve the problem of not being able to alert there) or an angular expression. it looks like angular does not allow you to use global scope methods in there (it might be looking them up in the current $scope, from which they are missing).

like image 62
orcaman Avatar answered Oct 08 '22 11:10

orcaman


ng-click expects an angular expression. Internally, angular is using the $parse service to evaluate the expression in ng-click="expression".

An angular expression is not the same as regular javascript code. $parse uses string parsing to interpret the expression and it restricts your access to variables, functions, and objects to just those which are properties of the $scope object, or properties of any $parent scope objects which happen to be available further up the prototypical inheritance chain.

So in theory you could gain access to globals like this:

 $scope.window = window;  $scope.alert = alert; 

... and then in your template do ng-click="window.alert('hello!')"

...or... ng-click="alert('hello!')"


Or you could do this just once:

 $rootScope.window = window;  $rootScope.alert = alert; 

Then any scope that prototypically inherits from $rootScope will also have access to window and alert.


... but there are good reasons never to do either of the above, except possibly for debugging purposes. That's not to say that decorating the $rootScope is always a bad idea, there is at least one special case where decorating angular's scope can accomplish something that would be very difficult to do otherwise.

like image 36
Gil Birman Avatar answered Oct 08 '22 09:10

Gil Birman