错误:$ injector:unpr未知提供程序,当尝试删除记录时

时间:2017-06-05 23:22:42

标签: javascript asp.net angularjs

我将使用angular js 1.3.13删除我的asp.net mvc项目中的记录。

 <tr ng-repeat="emp in employees track by $index">
            <td>
                <a href="#" class="btn btn-default btn-sm">
                    <i class="glyphicon glyphicon-edit"></i>
                </a>
            </td>
            <td>
                {{emp.EmployeeId}}
            </td>
            <td>
                {{emp.EmployeeName}}
            </td>
            <td>
                {{emp.Address}}
            </td>
            <td>
                {{emp.EmailId}}
            </td>
            <td>
                <a href="#" 
                   ng-click="delFn($index)"
                   class="btn btn-default btn-sm">
                    <i class="glyphicon glyphicon-trash"></i>
                </a>
            </td>
        </tr>

我的controller.js代码

app.controller("EmpCtrl", function ($scope, EmployeeService) {

GetAllEmployee();

function GetAllEmployee() {

    var getAllEmployee = EmployeeService.getEmployee();
    getAllEmployee.then(function (emp) {
        $scope.employees = emp.data;
    }, function () {
        alert('Data not found');
    });
}
    $scope.delFn = function (index) {
        var response = confirm('Delete this Employee?');
        if (response) {
           $scope.employees.splice(index, 1);
           EmployeeService.deleteEmployee(index);
        }
     }
});

在我的service.js中,我有

    app.service("EmployeeService", function ($http,id) {
    this.getEmployee = function () {
        return $http.get("/Employee/GetAllEmployee");
    };

    this.deleteEmployee = function () {
        var url = "/Employee/DeleteEmployee/" + id;
        $http.delete(url)
            .success(function (result) {
                alert("Delete Successfully");
            })
            .error(function () {
                alert("Something wrong!");
            })
            .then(function () {
                window.location = '#/';
            })
    }
});

在我的asp.net控制器中,我有删除方法

 [HttpDelete]
    public void DeleteEmployee(int id)
    {
       // blah
    }

但是我在chrome中调试时遇到错误。

  

angular.min.js:102错误:[$ injector:unpr] http://errors.angularjs.org/1.3.13/$injector/unpr?p0=idProvider%20%3C-%20id%20%3C-%20EmployeeService

那么请你告诉我什么是错的?

2 个答案:

答案 0 :(得分:2)

id参数从服务构造函数移动到deleteEmployee函数:

    //REMOVE `id` argument
    //app.service("EmployeeService", function ($http,id) {
    app.service("EmployeeService", function ($http) {
    this.getEmployee = function () {
        return $http.get("/Employee/GetAllEmployee");
    };

    //ADD `id` here
    //this.deleteEmployee = function () {
    this.deleteEmployee = function (id) {
        var url = "/Employee/DeleteEmployee" + id;
        return $http.delete(url)
            .then(function (response) {
                alert("Delete Successfully");
                return response;
            })
            .catch(function (errorResponse) {
                alert("Something wrong!");
                throw errorResponse;
            })
            .then(function () {
                window.location = '#/';
            })
    }
});

来自error message link

Unknown provider: idProvider <- id <- EmployeeService

注入器错误地尝试注入名为id的服务。

答案 1 :(得分:-1)

app.controller("EmpCtrl", ['$scope', 'EmployeeService', function ($scope, EmployeeService) {
//do stuff here
}]);

根据角度文档,您的服务注入未正确完成。尝试将控制器更改为上面的内容并查看错误是否消失