Fima Taf Fima Taf - 1 year ago 68
AngularJS Question

How to do if statment in AngularJS controller

I have an angular controller, in which i have a function that should return an image depends on the json that is received from the server.
The problem is that it does not matter which country is received it load only the first one (usa.png in my case).

$scope.getUser = function(){
url = "/get";
$scope.img = "";
$, {
function (response){
$scope.returnedUser =;
if ($ = "USA"){
$scope.img = "/base_icons/usa.png";
} else if ($ = "Canada"){
$scope.img = "/base_icons/canada.png";
} else if ($ = "Mexico"){
$scope.img = "/base_icons/mexico.png";
}, $scope.negativeMessage);};

<img ng-src="{{img}}"/>

Answer Source

You aren't doing equality:

if ($ = "USA") // this is an assignment operator in if

Should be:

if ($ == "USA")

Or you can have strict equality (recommended):

if ($ === "USA")

Strict equality is good (in most cases), because it means, things like '1' === 1 don't return true, where as '1' == 1 would return true.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download