You can do this with a simple directive.
Here is the HTML:
<!DOCTYPE html> <html ng-app="App"> <head> <link rel="stylesheet" href="style.css"> <script src="http://code.angularjs.org/1.2.0-rc.2/angular.js"></script> <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script> <script src="script.js"></script> </head> <body ng-controller="MyCtrl"> <h1>Hello Plunker!</h1> <img ng-directive /> </body> </html>
Here is the directive with the controller:
angular.module('App', []) .controller('MyCtrl', function($scope){ $scope.image = {name: "myName"}; }); angular.module('App') .directive('ngDirective', function($compile){ return { link: function(scope, element, attrs){ if(scope.image.name != null){ $compile($(element).attr('ng-src', 'http://lauterry.imtqy.com/slides-prez-angular/img/angularjs.png'))(scope); } } } });
Here is a complete working example: http://plnkr.co/edit/LNgsuX
source share