I cannot view object data returning from $http.get in the view

Asked

Viewed 374 times

0

I’m getting data from an api via $http request. This is my controller.

angular.module('supernovaWebApp',['ui.router'])
  .controller('professoresCtrl', ['$scope', '$http', function($scope, $http){
        $http.get("http://localhost:3000/api/instructors/"+instructorId).success(function(data,status){
          console.log(data);
          $scope.professor = data;        
        });
      };
  }])
  .config(function($stateProvider){
    $stateProvider.state('instructor', {
      url: '/instructor',
      templateUrl: 'views/instructor.html',
      controller: 'professoresCtrl'
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

The object returns without errors and I can observe the object in the console, but it does not render in the view.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div>
  <div class="mdl-cell mdl-cell--4-col">
    <div  class="mdl-card mdl-shadow--2dp">
      <div class="mdl-card__title">
        <h2 class="mdl-card__title-text">{{professor.name}} {{professor.last_name}}</h2>
      </div>
      <div class="mdl-card__supporting-text">
        <p>{{professor.profession}}</p>
        <i class="material-icons md-18">phone</i> {{professor.phone}}
      </div>
      <div class="mdl-card__menu">
        <button ng-click="delInstructor(instructor)" class="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect" >
          <i id="btn-archive" class="material-icons" >clear</i>
          <span class="mdl-tooltip" data-mdl-for="btn-archive">
            Arquivar
          </span>
        </button>
      </div>
    </div>
  </div>        
</div>

The strange thing is that when I make the same call to a list of objects, I can display them using ng-repeat, no problem. Does anyone know where I’m going wrong?

Console Print. The Object in question is id 1 Print do console

  • data returns a single object or a list? If possible, print `console(date)``

  • The instruction console.log(data); returns something? If not, my bet is that it is a CORS problem and your call is failing. Since there is no callback for the error case, you are not seeing the exception.

  • The console.log returns the object that should actually return. Once I get the screen print. @Lukssys

  • @Lukssys added the screen print.

  • This part of html you have placed is inside the view instructor? If yes, do a test: instead of setting the controller by the app at the angle, put in this view an ng-controller indicating the controller. Maybe your variable professor is in another scope!

  • @Christianfelipe had already done this test. It’s still the same. The controller is being defined by ui-router when it loads the Instructor view.

  • In your example you don’t have the attribute ng-app in HTML

  • @Sorack is in the html index. Inside the index I have <div ui-view> to render the views that the router takes over.

  • @Viniciusscaramel see if you can get some feedback with "{{data.professor.name}}".

Show 4 more comments
No answers

Browser other questions tagged

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