gpt4 book ai didi

javascript - 错误: $injector:unpr Unknown Provider when try delete a record

转载 作者:行者123 更新时间:2023-12-03 04:09:45 25 4
gpt4 key购买 nike

我将使用 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 Controller 中,我有删除方法

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

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

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

请您告诉我哪里出了问题?

最佳答案

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

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

关于javascript - 错误: $injector:unpr Unknown Provider when try delete a record,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44379119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com