0

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 = "";
		$http.post(url, {
			"Id":$scope.Id,
			"name":$scope.name
		}).then(
				function (response){
				$scope.returnedUser = response.data;
				if ($scope.returnedUser.country = "USA"){
					$scope.img = "/base_icons/usa.png";
				} else if ($scope.returnedUser.country = "Canada"){
					$scope.img = "/base_icons/canada.png";
				} else if ($scope.returnedUser.country = "Mexico"){
                    $scope.img = "/base_icons/mexico.png";
                }
				}, $scope.negativeMessage);};
<img ng-src="{{img}}"/>

0

3 Answers 3

6

You aren't doing equality:

if ($scope.returnedUser.country = "USA") // this is an assignment operator in if

Should be:

if ($scope.returnedUser.country == "USA")

Or you can have strict equality (recommended):

if ($scope.returnedUser.country === "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.

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

2 Comments

Cant believe that it was my mistake, I thought that it is somethng more complicated, Thank you!
It happens to the best of us mate, don't worry!
1

You assign value, but you have to check it:

$scope.returnedUser.country == "USA"

Comments

0

You can use as this way if countries are already fixed from particular group of countries and comparing is not compulsory.

$scope.image_base = "/base_icons/";
$scope.returnedUser = response.data;
$scope.img = $scope.image_base + ($scope.returnedUser.country).toLowerCase() + ".png";

Regards.

Comments

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.