AngularJS - 获取错误[ng:areq] http://errors.angularjs.org/1.3.2/ng/areq?p0=DbController

时间:2018-03-07 22:42:35

标签: php html angularjs

我正在研究CRUD angularApp但在Chrome开发工具下遇到奇怪的错误。以下是完整错误:

error: [ng:areq] http://errors.angularjs.org/1.3.2/ng/areq?p0=DbController&p1=not%20a%20function%2C%20got%20string
at angular.js:38
at Nb (angular.js:1577)
at ob (angular.js:1587)

我试过看DbController,但我似乎无法找到任何问题。我将包括对DbController的所有引用。

的index.php

<html ng-app="crudApp">
...  <!-- stylesheet and scripts added -->
<body>
<div class="container wrapper" ng-controller="DbController">
...

的script.js

    // Application module
var crudApp = angular.module('crudApp', []);
crudApp.controller("DbController", ['$scope', '$http', function($scope, $http) {

    // Function to get employee details from the database
    getInfo();

    function getInfo() {
        // Sending request to EmpDetails.php files
        $http.post('databaseFiles/empDetails.php').success(function(data) {
            // Stored the returned data into scope
            $scope.details = data;
        });
    }

    $scope.insertInfo = function(info) {
        $http.post('databaseFiles/insertDetails.php', {
            "name": info.name,
            "email": info.email,
            "address": info.address,
            "gender": info.gender
        }).success(function(data) {
            if (data == true) {
                getInfo();
                // Hide details insertion form
                $('#empForm').css('display', 'none');
            }
        });
    };

    $scope.currentUser = {};
    $scope.editInfo = function(info) {
        $scope.currentUser = info;
        $('#empForm').slideUp();
        $('#editForm').slideToggle();
    };

    $scope.UpdateInfo = function(info) {
        $http.post('databaseFiles/updateDetails.php', {
            "id": info.emp_id,
            "name": info.emp_name,
            "email": info.emp_email,
            "address": info.emp_address,
            "gender": info.emp_gender
        }).success(function(data) {
            $scope.show_form = true;
            if (data == true) {
                getInfo();
            }
        });
    };

    $scope.deleteInfo = function(info) {
        $http.post('databaseFiles/deleteDetails.php', {
            "del_id": info.emp_id
        }).success(function(data) {
            if (data == true) {
                getInfo();
            }
        });

    };
}]);

如果您在DBController代码中发现任何问题,请告诉我。

2 个答案:

答案 0 :(得分:-1)

您的控制器定义存在语法问题。这是不正确的:

crudApp.controller("DbController", ['$scope'], '$http', function($scope, $http) {
  ...
});

这应该是它的样子:

crudApp.controller("DbController", ["$scope", "$http", function($scope, $http) {
  ...
}]);

虽然,我更喜欢这个:

crudApp.controller("DbController", DbController);
DbController.$inject = ["$scope", "$http"];
function DbController($scope, $http) {
  ...
}

答案 1 :(得分:-1)

我对您的控制器进行了一些更改,它可以使用您提供的代码

// Application module
var crudApp = angular.module('crudApp', []);
crudApp.controller("DbController", ['$scope', '$http', function($scope, $http) {

    // Function to get employee details from the database
    getInfo();

    function getInfo() {
        // Sending request to EmpDetails.php files
        $http.post('databaseFiles/empDetails.php').success(function(data) {
            // Stored the returned data into scope
            $scope.details = data;
        });
    }

    $scope.insertInfo = function(info) {
        $http.post('databaseFiles/insertDetails.php', {
            "name": info.name,
            "email": info.email,
            "address": info.address,
            "gender": info.gender
        }).success(function(data) {
            if (data == true) {
                getInfo();
                // Hide details insertion form
                $('#empForm').css('display', 'none');
            }
        });
    };

    $scope.currentUser = {};
    $scope.editInfo = function(info) {
        $scope.currentUser = info;
        $('#empForm').slideUp();
        $('#editForm').slideToggle();
    };

    $scope.UpdateInfo = function(info) {
        $http.post('databaseFiles/updateDetails.php', {
            "id": info.emp_id,
            "name": info.emp_name,
            "email": info.emp_email,
            "address": info.emp_address,
            "gender": info.emp_gender
        }).success(function(data) {
            $scope.show_form = true;
            if (data == true) {
                getInfo();
            }
        });
    };

    $scope.deleteInfo = function(info) {
        $http.post('databaseFiles/deleteDetails.php', {
            "del_id": info.emp_id
        }).success(function(data) {
            if (data == true) {
                getInfo();
            }
        });

    };
}]);