dsz90288 2018-11-22 09:12
浏览 71

如何从AngularJS Script获取路由参数数据到php

I checked that my AngularJS Script is working fine. But when i run my php it tells me that index is undefined. I am sure i am not able to transfer data from angular js to php. So please Help me with this thing. I just want to know how can i get data from Angular to php using ($routeParams) so that i can work with it in BackEnd. Index.html

<!DOCTYPE html>
<html lang="en" ng-app="mod">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>webapp-angular</title>
<script src="scripts/angular.min.js"></script>
<script src="scripts/angular-route.js"></script>
<link rel="stylesheet" href="style.css">
<script src="scripts/script.js"></script>
<base href="/angular/app-route1/"/>
</head>
<body>
<header>
    <div class="brand">TDS</div>
    <ul>
        <li><a href="home">Home</a></li>
        <li><a href="courses">Courses</a></li>
        <li><a href="employees">Employees</a></li>
    </ul>
</header>
<section class="center">
    <ng-view></ng-view>
</section>
</body>
</html>

employeeDetails.html

<h1>employee List</h1>
<table>
        <thead>
                <tr>
                    <td>id</td>
                    <td>Name</td>
                    <td>gender</td>
                    <td>salary</td>
                </tr>
        </thead>
        <tbody>
                <tr>
                        <td>{{employee.id}}</td>
                        <td>{{employee.name}}</td>
                        <td>{{employee.gender}}</td>
                        <td>{{employee.salary}}</td>
                </tr>
        </tbody>
</table>
<a href="employees">Back to the list</a>

employee.html

<h1>Employees List</h1>
<ul>
    <li ng-repeat="employee in employees">
        <a href="employees/{{employee.id}}">{{employee.name}}</a>
    </li>
</ul>

new111.php

<?php 
include_once('connect.php');

$id = $_GET['id'];
if($conn && $id){
    $output = array();
    $query = "select * from employees where id='.$id.'";
    $result = mysqli_query($conn,$query);
    if(mysqli_num_rows($result)>0){
        while($row=mysqli_fetch_assoc($result)){
            $output[]=$row;
        echo json_encode($output);
    }
}
}
    else{
        echo "ERROR";
    }
?>

Script.js

var myApp=angular
                .module("mod",["ngRoute"])
                .config(["$routeProvider","$locationProvider",function($routeProvider,$locationProvider){
                    $routeProvider
                    .when("/home",{
                        templateUrl:"home.html",
                        controller:"homeController"
                    })
                    .when("/employees",{
                        templateUrl:"employees.html",
                        controller:"employeeController"
                    })
                    .when("/courses",{
                        templateUrl:"courses.html",
                        controller:"courseController"
                    })
                    .when("/employees/:id",{
                        templateUrl:"employeeDetails.html",
                        controller:"employeeDetails"
                    })
                    .otherwise({
                        redirectTo:"/home"
                    })
                    $locationProvider.html5Mode(true);
                }])
                .controller("homeController",function($scope){
                    $scope.message="Home Page";
                })
                .controller("employeeController",function($scope,$http){
                    $http.get("php/new11.php")
                    .then(function(response){
                        $scope.employees=response.data;
                    })
                })
                .controller("courseController",function($scope,$http){
                    $http.get("php/new1.php")
                    .then(function(response){
                        $scope.courses=response.data;
                    })
                })
                .controller("employeeDetails",['$scope','$http','$routeParams',function($scope,$http,$routeParams){
                    console.log($routeParams.id)
                    $http({
                        method:'get',
                        url:"php/new111.php",
                        params:{id:$routeParams.id}
                    })
                    .then(function(response){
                        $scope.employee=response.data;
                    })
                }])
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试