如何在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 ifShould 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.
更多推荐
发布评论