0

I want to show an image instead of text according to the data coming in the response of an API.

  • If true - access symbol,
  • If false - deny symbol should be shown.

I tried but I am getting the link in the display not the image.

<div ng-app="myApp" ng-controller="customersCtrl">
  <table>
    <tr>
      <td>device id</td>
      <td>device unique id</td>
      <td>access status</td>
      <td>current time</td>
    </tr>
    <tr>
      <td>{{ access.id }}</td>
      <td>{{ access.devid }}</td>
      <!--<td>{{ access.status }}</td>-->
      <td>{{statusValues[access.status]}}</td>
      <td>{{ access.CurrentTime }}</td>
    </tr>
  </table>
</div>
</div>
  <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function ($scope, $http) {
      $http.get("http://example.com/get")
        .then(function (response) {
        $scope.access = response.data.sensorsdata;
        $scope.statusValues = {
          'true': 'http://icons.iconarchive.com/icons/custom-icon-design/flatastic-9/256/Accept-icon.png',
          'false': 'https://cdn3.iconfinder.com/data/icons/musthave/256/Cancel.png',
        }
      });
    });

5 Answers 5

2

Simpy add the img tag with ng-src like so:

<div ng-app="myApp" ng-controller="customersCtrl">
      <table>
          <tr>
              <td>device id</td>
              <td>device unique id</td>
              <td>access status</td>
              <td>current time</td>
          </tr>
          <tr>
              <td>{{ access.id }}</td>
              <td>{{ access.devid }}</td>
              <td><img ng-src="{{statusValues[access.status]}}"/></td> 
              <td>{{ access.CurrentTime }}</td>
          </tr>
     </table>
</div>

<script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function ($scope, $http) {
        $http.get("http://example.com/get")
            .then(function (response) {
                $scope.access = response.data.sensorsdata; 
                $scope.statusValues = {
                    'true': 'http://icons.iconarchive.com/icons/custom-icon-design/flatastic-9/256/Accept-icon.png',
                    'false': 'https://cdn3.iconfinder.com/data/icons/musthave/256/Cancel.png',
            }
        });    
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the following synthax in your ngSrc:

<img ng-src="{{statusValues[access.status]}}"/>

Comments

1
<td><img ng-src="{{statusValues[access.status]}}"/></td>

you are missing an image tag

1 Comment

@Swapna you are welcome. If this answer was helpful please upvote and accept as answer. thank you
1

Use

<img ng-src="{{statusValues[access.status]}}"/>

Comments

1

Try with this code:

  <div ng-app="myApp" ng-controller="customersCtrl">
          <table>
              <tr>
                  <td>device id</td>
                  <td>device unique id</td>
                  <td>access status</td>
                  <td>current time</td>
              </tr>
              <tr>
                  <td>{{ access.id }}</td>
                  <td>{{ access.devid }}</td>
                  <td><img ng-src="{{statusValues[access.status]}}"/></td>
                  <td>{{ access.CurrentTime }}</td>
              </tr>
         </table>
    </div>

    <script>
        var app = angular.module('myApp', []);
        app.controller('customersCtrl', function ($scope, $http) {
            $http.get("http://example.com/get")
                .then(function (response) {
                    $scope.access = response.data.sensorsdata; 
                    $scope.statusValues = {
                        'true': 'http://icons.iconarchive.com/icons/custom-icon-design/flatastic-9/256/Accept-icon.png',
                        'false': 'https://cdn3.iconfinder.com/data/icons/musthave/256/Cancel.png',
                }
            });    
        });
    </script>

You had been missing the closing for <img> tag "/>

8 Comments

can u tell me how to refresh this page in javascript function.
<a href="javascript:location.reload(true)">Refresh Page</a> or location.reload(); helps you to refresh your current page.
<a href="javascript:location.reload(true)"> <table>...</table></a>.can u write like this ?
do u want to reload your table or full page?
need to refresh only one table. is it possible to refresh if the data is changed int the database. so that we can avoid page refresh in each second
|

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.