如何使用http post AngularJS将数据从php返回到页面?

My AngularJs page

  var postContactFn = function($http,data){
        return $http.post("myfirstfile.php", data);
    }

mainModule.factory('postContact',['$http',postContactFn]);

//In my controller

    postContactFn(data).success(function(data, status) {
        if(data){
                  console.log('success');
          }
    }

Php page MyFirstFile.php

 <?php
$name = $_POST['inputUsername']; 
if(!empty($name)){
  --send mail
 echo 'success'
}

Here the mail is been sent to my id but I'm not getting redirected back the controller. Instead it is showing success on my page.

Any suggestions would be appreciated.. Thank you

You pass the data as dependency, not to a function.

This is how it should be:

mainModule.controller('MyController', function($scope, postContact) {
  postContact.postData(data).then(function(data)) {
    console.log(data);
  });
});

And your factory:

mainModule.factory('postContact', function($http) {
  return {
    postData : function(data) {
      return $http.post("myfirstfile.php", data);
    }
  }
});