58

I have been trying to bind a value to the ng-src of an img HTML element to no avail.

HTML code:

<div ng-controller='footerCtrl'>
<a href="#"><img ng-src="{{avatar_url}}"/></a>
</div>

AngularJS code:

app.controller('footerCtrl',function($scope, userServices)
{
$scope.avatar_url='';
$scope.$on('updateAvatar', function()
{$scope.avatar_url = userServices.getAvatar_url();}
);
}

app.factory('userServices', function($rootScope){ 
var avatar_url='';
return{  setAvatar_url: function(newAvatar_url)
{ avatar_url = newAvatar_url; $rootScope.$broadcast('updateAvatar');}}
);

I would like to update the avatar_url variable in the ng-src every-time its respective variable(avatar_url) in the user Service is updated. The variable in the user Service is updated through a http.POST request to the server. I have checked that the response from the server does update the variable in the user Service which is then broadcast to the avatar_url variable in the footerCtrl.

However, the image element HTML does not reflect the changes at all. In fact, I have also tried to preset the avatar_url variable to a relative path to one of the pictures in my page, the image still shows nothing(the ng-src value is empty). T

4
  • In the userServices you have a setAvatar_url function vs. in the controller you use the getAvatar_url function. Could that be the issue? Commented May 6, 2013 at 9:39
  • Hi, sorry for the delay in replying, maybe the fiddle jsfiddle.net/jiaming/DvNA9/1 would be clearer in explaining the code. Thank you for your time. Commented May 6, 2013 at 13:17
  • Your jsfiddle is full of bugs... Commented May 6, 2013 at 14:59
  • I was looking ng-src attribute for usage, and question already gave me the answer thanks! :) Commented Feb 11, 2014 at 6:10

2 Answers 2

91

Changing the ng-src value is actually very simple. Like this:

<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
</head>
<body>
<img ng-src="{{img_url}}">
<button ng-click="img_url = 'https://farm4.staticflickr.com/3261/2801924702_ffbdeda927_d.jpg'">Click</button>
</body>
</html>

Here is a jsFiddle of a working example: http://jsfiddle.net/Hx7B9/2/

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. this was what I was really trying to do: jsfiddle.net/jiaming/DvNA9/12 . But the main problem was that I was rendering it as a jinja template in the app engine server which caused conflicts with all the {{}} braces.
Oh, that you mean. You can change the {{}} if you like: jsfiddle.net/THjQe/1
wow, thanks for that reply, I didn't know you can do that. (:
You might want to be sure that you are not breaking any plugins before you change Angular's {{}}. There are some that relies on {{}}.
8

We can use ng-src but when ng-src's value became null, '' or undefined, ng-src will not work. So just use ng-if for this case:

http://jsfiddle.net/Hx7B9/299/

<div ng-app>
    <div ng-controller="AppCtrl"> 
        <a href='#'><img ng-src="{{link}}" ng-if="!!link"/></a>
        <button ng-click="changeLink()">Change Image</button>
    </div>
</div>

1 Comment

Thank you. This is a bug. Had me confused. ng-src was displaying a previous image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.