0

I have this scope function

$scope.foo = function() { console.log('foo') } 

What is the shortest way of calling it in a DOM event handler?

<img src='foo.jpg' onError="???"> 

Please note that the code is a sample. I don't need to know how to handle failing images.

2

2 Answers 2

1

This solves the issue:

<img src='foo.jpg' onError="angular.element(this).scope().foo()"> 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes it does the job but has bad affects with scope nesting because of this context.You may want to select the right element for right scope variable. Please see jsfiddle.net/Lvc0u55v/3314 for better understanding.
It will automatically select the right controller if you use this
It's selects the right controller ,but it may not be the controller you would be expecting to get selected.please see the jsfiddle for better explanation.
angular.element(this).scope() always selects the right scope even when there are child scopes. Please observe the fiddle carefully.
-1

Please see following code.Using this approach scope function can be called without any nested scoping issue.

 <div id="myctrl" ng-controller="MyCtrl"> <div ng-controller="DemoCtrl"> <img src='foo.jpg'onError="angular.element(document.getElementById('myctrl')).scope().foo()"> </div> </div> 

4 Comments

This is not what I asked. Please read the question well. Please use comments for commentary.
You mentioned code is just a sample. So I answered in generic manner.
Correction its {{foo()}}
that doesn't work. Please test your code before publishing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.