Angularjs $http.get does not work

£可爱£侵袭症+ 提交于 2019-12-05 17:13:44

If I understood correctly getUsersFromLocal function is inside controller, basically the function parameters are killing the $scope, $http object existence, You need to remove them from parameter & Also removed the return statement which was outside $http which won't work in anyways.

Code

app.controller('mainCtrl', function() {
    $scope.getUsersFromLocal = function() {
        $http.get('http://localhost:8080/people').
        success(function(data) {
            $scope.data = data;
        });
    };

    $scope.getUsersFromLocal(); //call ajax method
});

Try like this , this way i found on w3school.

var app = angular.module('myApp4', []);
    app.controller('customersCtrl', function($scope, $http) {
        $http.get("http://www.w3schools.com/angular/customers.php")
                .success(function(response) {
                    $scope.data= response.records;
                });
    });

if getUsersFromLocal is not a controller or service, and you are invoking this function manually, you should pass $http and $scope objects to it, like this

module.controller('TestController', function($scope, $http) {

  function getUsersFromLocal($scope,$http) {
    $http.get('http://localhost:8080/people').
      success(function(data) {
        $scope.data = data;
      });
  }

  getUsersFromLocal($scope, $http); // pass this services manually
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!