One option uses directive and uses a method called save, which will handle the src attribute found in the image tag.
Js
var app = angular.module('myApp', []); app.directive('saveImage', function () { return { transclude: true, link: function (s, e, a, c) { s.save=function(){ alert(a.src); }; } }; });
HTML
<div > <img save-image style="width: 300px; height: 200px;" src="http://placehold.it/350x150"> <a ng-click="save()" class="btn">Submit</a> </div>
This is the code implemented in jsfiddle.
Another option is to isolate the scope to the controller, but still apply the image to it instead of the function.
Js
var app = angular.module('myApp', []); app.directive('saveImage', function () { return { transclude: true, link: function (s, e, a, c) { s.image = a.src; } }; }); function cntl($scope) { $scope.save = function (img) { alert($scope.image || 'no image'); } }
HTML
<div ng-controller='cntl'> <img save-image style="width: 300px; height: 200px;" src="http://placehold.it/350x150"> <a ng-click="save()" class="btn">Submit</a> </div>
Note the added ng-controller = "cntl".
This is JSfiddle for this.
mitch source share