我想通过我的 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
asfunction
and also you have that again equal toresponse.data
(actually you miss thefunction
)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/