我是Angular和MVC的新手,我已经创建了一个显示代码 记录到表格视图中。
用户可以通过单击“编辑”和“删除”来编辑和删除该记录 链路
但是当我点击编辑链接 TypeError时,它会显示错误: v2.UpdataData不是函数
它还显示错误 TypeError:$ http.get不是函数 当我点击删除链接
任何人都可以帮我解决这个问题。提前谢谢。
Controller.js
//update data of table
$scope.UpdataData = [];
//UpdateData();
$scope.UpdateData = function (StateMaster) {
var RetValData = DataService.UpdateStudent(StateMaster);
debugger;
getData.then(function (StateMaster) {
debugger;
StateID: $scope.StateID;
StateName: $scope.StateName;
CountryName: $scope.CountryName;
}, function () {
alert('Error in getting records');
});
}
$scope.DeleteData = [];
$scope.DeleteData = function (r, index) {
var retval = DataService.DeleteData(r.StateID).success(function (msg) {
$scope.countrydata.splice(index, 1);
alert('Student has been deleted successfully.');
}).error(function () {
alert('Oops! something went wrong.');
});
}.factory('DataService', function ($http) {
var fac={}
debugger;
fac.UpdateData= function($http)
{
return $http.get('/State/UpdateData');
}
return fac;
}).factory('DataService', function () {
var fac={}
debugger;
fac.DeleteData = function ($http)
{
return $http.get('/State/DeleteData');
}
return fac;
});
StateController.cs
public string UpdateData(StateMaster r)
{
if (r != null)
{
using (simpleEntities dbContext = new simpleEntities())
{
StateMaster lstdata = dbContext.StateMasters.Where(x => x.StateID == r.StateID).FirstOrDefault();
lstdata.StateName = r.StateName;
lstdata.CountryName = r.CountryName;
dbContext.SaveChanges();
return "Data Updated";
}
}
else
{
return "something went worng";
}
}
[HttpPost]
public string DeleteData(int id)
{
if (id != 0)
{
using (simpleEntities databContext = new simpleEntities())
{
var lsdata = databContext.StateMasters.Where(x => x.StateID == id).FirstOrDefault();
databContext.StateMasters.Remove(lsdata);
databContext.SaveChanges();
return "Data Deleted Successfully";
}
}
else {
return "Error Occured";
}
}
InsertState.cshtml
<div ng-app="MyApp" ng-controller="StateController">
<table class="table table-bordered">
<tr>
<td>Sr. No.</td>
<td>Country Name</td>
<td>State Name</td>
<td></td>
@*<td>Country Name</td>*@
</tr>
<tr ng-repeat="r in countrydata">
<td>{{$index + 1}}</td>
<td>{{r.CountryName}}</td>
<td>{{r.StateName}}</td>
@*<td>{{r.CountryName}}</td>*@
<td>
<span ng-click="UpdataData(r)" class="btnAdd">Edit</span> |
<span ng-click="DeleteData(r)" class="btnRed">Delete</span>
</td>
</tr>
</table>
</div>
答案 0 :(得分:1)
您的代码中存在此问题
line $scope.UpdateData = function (StateMaster
因为你正试图运行你的$ scop功能但功能是工厂所以你必须尝试
factory.UpdateDate
似乎在角度定义factory
时存在问题,您可以尝试以下方式
var app = angular.module('myApp', []);
app.factory('testFactory', function(){
return {
sayHello: function(text){
return "Factory says \"Hello " + text + "\"";
},
sayGoodbye: function(text){
return "Factory says \"Goodbye " + text + "\"";
}
}
});
function HelloCtrl($scope, testService, testFactory)
{
$scope.fromFactory = testFactory.sayHello("World");
}
你可以放置你的功能,只是尝试它返回你的价值