简体   繁体   中英

how to change the data in the plain text according to the response coming in json data in angular js

im getting json data in the response as it is coming from response.but i need to change the data like this if 0: registered, 1: connected, 2: disconnected, 3: busy in the plain text. im able to show the data as it is coming in response, but i need to modify according to the value getting in the response. conditions

0: registered, 1: connected, 2: disconnected, 3: busy

op - status is- 3

desired op- status busy

sample json data from api

"status":3

code

 <div ng-app="myApp" ng-controller="myCtrl" align="center">

     <h2>status is-  {{statusValues[names.status]}}</h2>

        </div>

       <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope, $http) {
            $http.get('url', {
                headers: { 'Authorization': 'Basic a2VybmVsc3B==' }
            })
            .then(function (response) {
                $scope.names = response.data;
                $scope.statusValues={
                    '0' : 'registered',
                    '1' : 'connected',
                    '2': 'disconnected',
                    '3':'busy'
                };


            });
        });
</script>

it's not entirely clear what you are asking but I thing you want:

<h2>status is-  {{names.status ? 'Active' :'Inactive'}}</h2>

This is a javascript ternary and will render the word "Active" when status is truthy (==1) and "Inactive" when it is falsy (==0)


Based on update perhaps you want something like:

$scope.statusValues={
   '0' : 'registered',
   '1' : 'connected',
    ....
   '3' : 'busy'
}

Then in view:

<h2>status is-  {{statusValues[names.status]}}</h2>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM