Roxx
Roxx

Reputation: 3986

How can I get data automatically on page load?

I am trying to get the data from database using php and that data will be visible on my web page.

formApp.controller('getsettings', function($scope,$http){
      $scope.formData = {};

      $scope.getsettings = function() {
            var allData={'uid': uid}
        $http({
              method  : 'POST',
              url     : 'get_settings.php',
              data : allData,
              headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  

          })
              .success(function(data) {

                  if (!data.success) {
                       $scope.fname = data.fname;
                      $scope.lname = data.lname;
                      $scope.mobile = data.mobile;

                  }else{
                      $scope.fname = '';
                      $scope.lname = '';
                      $scope.mobile = '';

                    }
              });

      };

    })

Problem is above code is not firing. I checked the network tab there are no event.
How can i fire the event. Any advice what am i doing wrong?

One more question i have is many places i have seen people are using GET instead of POST in angular. Why is that?

Upvotes: 0

Views: 87

Answers (2)

Rahul Desai
Rahul Desai

Reputation: 15481

You have implemented the function $scope.getsettings but you have not called it anywhere.

You can invoke/call this function with $scope.getsettings() right after its implementation. Once the controller loads in the browser, this function will be invoked.

EDIT:

In response to your comment - corrected JS code:

formApp.controller('getsettings', function($scope,$http){
  $scope.formData = {};

  $scope.getsettings = function() {
    var allData={'uid': uid}
    $http({
      method  : 'POST',
      url     : 'get_settings.php',
      data : allData,
      headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  

    })
      .success(function(data) {

      if (!data.success) {
        $scope.fname = data.fname;
        $scope.lname = data.lname;
        $scope.mobile = data.mobile;

      }else{
        $scope.fname = '';
        $scope.lname = '';
        $scope.mobile = '';

      }
    });

  };

  // now call/invoke function $scope.getsettings
  $scope.getsettings();

});

Upvotes: 2

Hitesh Kumar
Hitesh Kumar

Reputation: 3698

The problem is you are not calling the $scope.getSettings function. You have simply created it. You can call it by $scope.greetings(). Or if you want to use it in your view then you can do something like this: <button ng-click="getSettings()">Get Setting</button>.

Upvotes: 1

Related Questions