1

i want to show particular image when value is greater than 3(float) . while less than 3 it should show different image. how to write condition in of comparing value and according to that need to show.

condition

value > 3.5 = http://icons.iconarchive.com/icons/custom-icon-design/flatastic-9/256/Accept-icon.png
value =< http://icons.iconarchive.com/icons/custom-icon-design/flatastic-9/256/Accept-icon.png 

code 
      <script>
           var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope, $http) {
            $http.get('https://example.com/get', {
                headers: { 'Authorization': 'Basic a2VybmVsc==' }
            })
               .then(function (response) {
                   $scope.names = response.data;
                   $scope.decodedFrame = atob($scope.names.dataFrame);
                   $scope.decodedFrameNew = $scope.decodedFrame.substring(4);
                   $scope.distanceinFeet = 835 * 0.95;
                   $scope.Value = $scope.distanceinFeet / 148;
                   $scope.parkingslot1 = $scope.Value.toFixed(2);
                   $scope.names.timestamp = new Date($scope.names.timestamp).toLocaleString(); // Parse the date to a localized string
               });


            alert("hi");
            $scope.getSlotImage = function (slot) {
                alert("hi");
                var imageUrl = slot > 3.5 ? 'http://icons.iconarchive.com/icons/custom-icon-design/flatastic-9/256/Accept-icon.png' :
                        'https://cdn3.iconfinder.com/data/icons/musthave/256/Cancel.png'
                alert("hi");

                return imageUrl;
                alert("hi");

            }
            });

    </script>

body

  <td><img ng-if ng-src="{{getSlotImage(parkingslot1)}}" /></td>
2
  • what value 3.5?? or 3?? Commented Feb 18, 2017 at 13:14
  • if greater than 3.00 Commented Feb 18, 2017 at 13:15

2 Answers 2

1

You could just call a function inside ng-src to get the relevant image. Below you can see, how it should look like your controller and view

View

No need ng-if here.

<td><img ng-src="{{getSlotImage(parkingslot1)}}" /></td>

Controller

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $http.get('https://example.com/get', {
      headers: {
        'Authorization': 'Basic a2VybmVsc=='
      }
    })
    .then(function(response) {
      $scope.names = response.data;
      $scope.decodedFrame = atob($scope.names.dataFrame);
      $scope.decodedFrameNew = $scope.decodedFrame.substring(4);
      $scope.distanceinFeet = 835 * 0.95;
      $scope.Value = $scope.distanceinFeet / 148;
      $scope.parkingslot1 = $scope.Value.toFixed(2);
      $scope.names.timestamp = new Date($scope.names.timestamp).toLocaleString(); // Parse the date to a localized string
    });

  $scope.getSlotImage = function(slot) {
    var imageUrl = slot > 3.5 ? 'http://icons.iconarchive.com/icons/custom-icon-design/flatastic-9/256/Accept-icon.png' : 'https://cdn3.iconfinder.com/data/icons/musthave/256/Cancel.png';

    return imageUrl;
  }
});

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

6 Comments

Value mean what you need to pass into, to do evaluate your logic to get the relevant image, in your case parkingslot1. Hope you got my idea
my sample value is 5.36. im not getting image.
It is working fine. I just check it. In controller I gave $scope.parkingslot1 = 5.36 and also loaded the Accept-icon.png
put $scope.getSlotImage function outside form your get request. It should inside your myCtrl scope
I just udated your code in question. I changed the place of getSlotImage function. Check it
|
0

Try this

<img ng-if="Value>3.5" ng-src="{{slot1image['>3.5']}}" /></td>  
<img ng-if="Value<=3.5" ng-src="{{slot1image['<=3.5']}}" /></td>  

Unfortunatly you can not use dot notation

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.