通过AngularJS http GET传递和访问参数

时间:2017-03-01 18:00:35

标签: javascript php mysql angularjs angular-http

http.get中的初学者: 我通过page.php?dept=some_dept&office=some_office将参数传递给url(php文件)。在我的php文件中,我有mysql查询,我必须在where子句中读取这些参数。

简而言之:http://example.com/page.php?dept=some_dept&office=some_office是我的页面,我有角度控制器和视图部分。从这里我想得到url string / params并传递给angular-data.php,从那里我正在编写响应。但是,angular-data.php有一个sql查询,我计划在WHERE子句中传递动态值,即在我的视图页面上获得响应之前来自角度控制器(部门和办公室)的参数(上面给出了带有参数的url)< / p>

到目前为止,我的代码如下:

var app = angular.module('myApp', []); app.controller('customersCtrl', function ($scope, $http) { $http({ method: 'GET', url: 'angular-data.php', data: { param1: type, param2: office } }).then(function (response) { $scope.names = response.data.records; }); });

<tr ng-repeat="x in names">
    <td>
        {{$index + 1}}
    </td>
    <td>{{ x.Name}}.</td>
    <td>{{ x.Office}}</td>
</tr>

page.php?dept = some_dept&amp; office = some_office(继续......):

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "root", "", "local_server");
$result = $conn->query("SELECT * FROM `table` WHERE `Dept` = " . $_GET['dept'] . " AND `Office` = " . $_GET['office'] . "");
$outp = "";
while ($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {
        $outp .= ",";
    }
    $outp .= '{"Name":"' . $rs["Name"] . '",';
    $outp .= '"Office":"' . $rs["Office"] . '"}';
}
$outp = '{"records":[' . $outp . ']}';
$conn->close();
echo($outp);

angular-data.php我希望动态参数为WHERE子句:

{{1}}

3 个答案:

答案 0 :(得分:1)

var app = angular.module('myApp', []);
        app.controller('customersCtrl', function ($scope, $http) {
            $http({
                method: 'GET',
                url: 'angular-data.php',
                params: {
            param1: 1,
            param2: 2
        }
            }).then(function (response) {
                $scope.names = response.data.records;
            });
        });

查看更多详情

AngularJS passing data to $http.get request

答案 1 :(得分:1)

我不明白,如果你在发送http请求之前在html中有一个param类型和office。例如:  

然后:

{{1}}

答案 2 :(得分:1)

在您的SQL查询中,您正在使用deptoffice。 您可以通过以下传递这些参数。

您可以使用params:将使用paramSerializer序列化并附加为GET参数的字符串或对象的映射。

$http({
          method: 'GET',
          url: 'angular-data.php',
          params: 'dept=some_dept, office=some_office'
    })

OR

$http({
          method: 'GET',
          url: 'angular-data.php',
          params: {
                     dept:"some_dept",
                     office:"some_office"
                  }
    })

可在此处找到更多内容$http#config