$http.get not working on controller init (solved)


#1

I have the following controller code for an “Item” page in my application. The itemId stateParams value is successfully passed through when an item is clicked.

The alert messages I see on page load are as follows:

index.html
http://127.0.0.1:8000/items/23/

index.html
’here’

And then the page finishes loading, and displays {{item.name}} instead of the item name.

The controller code is below:

.controller('ItemCtrl', function($scope, $stateParams) {

$scope.item_url = 'http://127.0.0.1:8000/items/' + $stateParams.itemId + '/';
alert($scope.item_url);


init = function (){
  alert("here");
  $http.get($scope.item_url)
    .success(function(data) {
      alert(data);
      $scope.item = data;
    })
    .error(function(data) {
      alert('error!');
    });
};

init();

})

Does anyone know why the $http.get function is not called? I don’t get a success OR return alert, but I do enter the init method.


#2

Of course, two seconds after I post, I discovered the issue. I needed to pass the $http variable into the controller.

.controller('ItemCtrl', function($scope, $http, $stateParams) {

#3

Good to see you to this resolve :smile: