Load JSON via Angularjs

Asked

Viewed 1,437 times

0

Guys, I’m trying to load a json object to display your information on the screen. The code I’m using is as follows::

(function() {
  var app = angular.module('tela', []);

  app.controller('TelaController', ['$http', function($http){
    var user = this; //testei com $http.get também
    $http.jsonp('info.json').success(function(data){
        user = data;
    }); 
  }]);

  var usuario = { 
    tipo: 'engenheiro', 
    maquina: 'XX160',   
    cliente: 'malhas',
    status: true
    };

})();

HTML

<!doctype html>
<html ng-app="tela">
<head>
<meta charset="UTF-8">
    <title>Tela</title>
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/layout.css">
    <script src="js/angular.min.js"></script>
    <script src="app.js"></script>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body ng-controller="TelaController as display">
    <header> <!-- cabeçalho -->
        <table align="center">
            <tbody>
                <tr>
                    <td rowspan="3"><img src="imagens/logo.png" alt="Audaces a tecnologia da moda"></td>
                    <td><b>Acesso: </b> {{display.user.tipo}}</td>
                    <td rowspan="3"><b>Status:</b><img src="imagens/positivo.png" alt="status" height="15" width="15" ng-show="display.user.status"><img src="imagens/negativo.png" alt="status" height="15" width="15" ng-show="!display.user.status">{{display.user.ativo}}</td>                    
                </tr>
                <tr>
                    <td><b>Maquina:</b> {{display.user.maquina}}</td>
                </tr>
                <tr>
                    <td><b>Cliente:</b> {{display.user.cliente}}</td>
                </tr>
            </tbody>
        </table>         
</header> 

When I was doing straight this.user = usuario it worked, but these data I will have to read constantly. They know how to solve?

  • 1

    Please explain the context better. Where the display variable is declared, what would it be? ;user in the part where you describe would be the same user that is inside the controller ?

  • @Felippetadeu display is an alias of the controller: ng-controller="TelaController as display"

  • What would be the constant ? A setInterval for example ?

1 answer

2

Try:

app.controller('TelaController', ['$http', function($http){
  this.user = $http.jsonp('info.json'); 
}]);

(I’m in no condition to test now, maybe I’m wrong)

  • It didn’t work, but thanks for trying

  • did not work because the return is a Promise and not a collection of data for example.

  • var source = this;&#xA;this.user;&#xA;$http.jsonp('info.json').then(function(data){&#xA; source.user = data;&#xA;});

  • @Felippetadeu I had the impression of having seen the angular take care of it "magically" ever. I must have been mistaken.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.