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/
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 (, ).
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With