ng:click fails silently

1,224 views
Skip to first unread message

Ruiwen Chua

unread,
Jan 14, 2011, 1:53:31 AM1/14/11
to Angular
Hi all,

I'm having a seriously weird issue attempting to use ng:click in my
code.

Even copying the basic example from the ng:click reference (http://
angularjs.org/Ng:click):


<input type="button" value="Hello" ng:click="$window.alert('hello')">


seems to do nothing when the button is clicked. No alert box, no
console error, nothing. Tested this both on Chrome (dev) and Safari
(5.0.3).

Is there something I'm missing? Other directives like ng:init and
ng:repeat seem to work fine in the code, along with the usual
databinding examples, just not ng:click.

I've also just updated to the latest 0.9.9 time-shift just to be sure,
but I'm still not getting it to work.

Thanks in advance if anyone has an idea what I might be doing wrong
here.

Cheers
Ruiwen

Igor Minar

unread,
Jan 14, 2011, 11:04:23 AM1/14/11
to ang...@googlegroups.com
Hi Ruiwen,

Sorry about the trouble, the example you are looking at is outdated
and it was a bad example altogether. A better one can be found here:
http://docs.angularjs.org/#!angular.directive.ng:click

In 0.9.9 we introduced several breaking changes [1] which were
necessary for any non-toy application.

If you wanted to really issue the alert command the code would look like this:

<!DOCTYPE HTML>
<html xmlns:ng="http://angularjs.org">
<head>
<script type="text/javascript"
src="http://code.angularjs.org/angular-0.9.9.js" ng:autobind></script>
</head>
<body>


<input type="button" value="Hello"

ng:click="$service('$window').alert('hello')">
</body>
</html>

if you needed access to $window more than once you should do this:

<!DOCTYPE HTML>
<html xmlns:ng="http://angularjs.org">
<head>
<script type="text/javascript"
src="http://code.angularjs.org/angular-0.9.9.js" ng:autobind></script>
</head>
<body ng:init="$window = $service('$window')">


<input type="button" value="Hello" ng:click="$window.alert('hello')">

</body>
</html>

In a real world app, you would have a controller and inject $window
into the controller like this:

function MyCtrl($window) {
this.$window = $window;
}
MyCtrl.$inject = ['$window'];


cheers,
Igor

[1] https://github.com/angular/angular.js/blob/v0.9.9/CHANGELOG.md

> --
> You received this message because you are subscribed to the Google Groups "Angular" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages