如何在AngularJS控制器中进行判断(How to do if statment in AngularJS controller)

编程入门 行业动态 更新时间:2024-10-27 16:33:41
如何在AngularJS控制器中进行判断(How to do if statment in AngularJS controller)

我有一个角度控制器,其中我有一个应该返回图像的函数取决于从服务器接收的json。 问题是接收哪个国家并不重要只加载第一个国家(在我的情况下是usa.png)。

$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}}"/> 
  
 

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}}"/> 
  
 

最满意答案

你没有做平等:

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

应该:

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

或者你可以有严格的平等(推荐):

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

严格的平等是好的(在大多数情况下),因为它意味着,像'1' === 1这样的东西不会返回true,而'1' == 1会返回true。

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.

更多推荐

本文发布于:2023-08-08 00:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   如何在   controller   AngularJS   statment

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!