0

我检查了我的 AngularJS 脚本工作正常。但是当我运行我的 php 时,它告诉我索引未定义。我确定我无法将数据从 angular js 传输到 php。所以请帮我解决这个问题。我只想知道如何使用 ($routeParams) 从 Angular 获取数据到 php,以便我可以在后端使用它。索引.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>

员工详情.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>

员工.html

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

新111.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";
    }
?>

脚本.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;
                    })
                }])
4

0 回答 0