javascript - 如何以 Angular 使用functions.php文件中的函数

标签 javascript php angularjs

我想通过我的 Angular 中的 PHP 中的函数检索用户信息。

Angular 代码:

$scope.users = $http({
  method: 'GET',
   url: 'functions.php/getUsers()'
  }).then(function(response){
   $scope.users = response.data;
  },function(reason){
   $scope.error = reason.data;
  });
}

其中 getUsers() 是将 json 数据返回到 Angular 的函数。

还有其他方法可以使用 PHP 文件中的 Angular 函数吗?

最佳答案

remember the functions can not be equal to our public variable, in this sample you have $scope.users as function and also you have that again equal to response.data (actually you miss the function)

you can't read from files, you have to use api URL which return json to you

$scope.users = $http({
  method: 'GET',
   url: 'functions.php/getUsers()'
  }).then(function(response){
   $scope.users = response.data;
  },function(reason){
   $scope.error = reason.data;
  });
}

更改为

$scope.getUsers = function(){
  //for example api url for your users is "http://localhost:8080/users"
  var api = "you api url";
  $http({method: 'GET', url: api}).then(function(response){
   $scope.users = response.data;
  },function(reason){
   $scope.error = reason.data;
  });
 }    
}
//request data
$scope.getUsers();

或者直接使用而不使用函数

//for example api url for your users is "http://localhost:8080/users"
var api = "you api url";
$http({method: 'GET', url: api}).then(function(response){
  $scope.users = response.data;
},function(reason){
  $scope.error = reason.data;
});
} 

关于javascript - 如何以 Angular 使用functions.php文件中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44468831/

相关文章:

php - 以小背景显示文本

php - 如何多次更新一个字段?

php - Mysql ORDER BY 数字 DESC

javascript - $viewValue 未应用于文本框指令?

javascript - Controller 内部的回调函数在 ng-options 之后完成

javascript - 通过nodejs中的url获取哈希

javascript - 如何使用 jQuery 或 javascript 编辑父标签的 html?

javascript - 如何只观察数组中的一个对象?

javascript - ng-repeat 过滤器未应用

javascript - 克隆一个 SVG 路径并将其放入另一个 SVG 中