gpt4 book ai didi

javascript - Angular JS 方法未使用 Wcf 服务定义

转载 作者:行者123 更新时间:2023-11-28 04:09:17 25 4
gpt4 key购买 nike

我正在 Angular Js 应用程序中使用 Wcf 服务。我的 Wcf 服务正在运行,我正在尝试显示 Sql 数据库中的用户记录列表。当我运行应用程序给我以下错误...

 angular.js:14642 TypeError: Cannot read property 'getAllStudent' of undefined
at GetAllRecords (Registration.js:13)

这是我的 Script.js 文件代码...

/// <reference path="../angular.min.js" />  



var app = angular.module("WebClientModule", [])

.controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService, CRUD_AngularJs_RESTService) {

$scope.OperType = 1;
GetAllRecords();
//To Get All Records
function GetAllRecords() {
var promiseGet = CRUD_AngularJs_RESTService.getAllStudent();
promiseGet.then(function (pl) { $scope.User = pl.data },
function (errorPl) {
$log.error('Some Error in Getting Records.', errorPl);
});
}
//1 Mean New Entry

//To Clear all input controls.
function ClearModels() {
$scope.OperType = 1;
$scope.Username = "";
$scope.Password = "";
$scopr.Email = "";

}


$scope.createuser = function () {
var User = {
Username: $scope.Username,
Password: $scope.Password,
Email: $scope.Email

};
if ($scope.OperType === 1) {
var promisePost = myService.post(User);
promisePost.then(function (pl) {
$scope.User_Id = pl.data.User_Id;
window.location.href = "/Login/Index";

ClearModels();
}, function (err) {
$scope.msg = "Password Incorrect !";
console.log("Some error Occured" + err);
});
}
}
}]);

app.service("myService", function ($http) {
//Create new record
this.post = function (User) {
var request = $http({
method: "post",
url: "http://localhost:52098/HalifaxIISService.svc/Register",
data: JSON.stringify(User)
});
return request;

this.getAllStudent = function () {
return $http.get("http://localhost:56766/StudentService.svc/GetAllStudent");
}
}


})

这是 HTML 代码..

@{
Layout = null;
}

<html data-ng-app="WebClientModule">
<head title="ASAS">
<title></title>
<script src="~/Scripts/angular.min.js"></script>

<script src="~/RegistrationScript/Registration.js"></script>


</head>
<body>
<table id="tblContainer" data-ng-controller="Web_Client_Controller">
<tr>
<td>
<table style="border: solid 2px Green; padding: 5px;">
<tr style="height: 30px; background-color: skyblue; color: maroon;">
<th></th>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Emial</th>

<th></th>
<th></th>
</tr>
<tbody data-ng-repeat="user in Users">
<tr>
<td></td>
<td><span>{{user.User_Id}}</span></td>
<td><span>{{user.Username}}</span></td>
<td><span>{{user.Password}}</span></td>
<td><span>{{user.Email}}</span></td>
<td>
<input type="button" id="Edit" value="Edit" data-ng-click="get(user)" />
</td>
<td>
<input type="button" id="Delete" value="Delete" data-ng-click="delete(user)" />
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<div style="color: red;">{{Message}}</div>
<table style="border: solid 4px Red; padding: 2px;">
<tr>
<td></td>
<td>
<span>User ID</span>
</td>
<td>
<input type="text" id="User_Id" readonly="readonly" data-ng-model="User_Id" />
</td>
</tr>
<tr>
<td></td>
<td>
<span>Username</span>
</td>
<td>
<input type="text" id="username" data-ng-model="Username" required="" />
</td>
</tr>
<tr>
<td></td>
<td>
<span>Password</span>
</td>
<td>
<input type="password" id="password" required data-ng-model="Password" require="" />
</td>
</tr>
<tr>
<td></td>
<td>
<span>Email</span>
</td>
<td>
<input type="email" id="email" required data-ng-model="Email" require="" />
</td>
</tr>

<tr>
<td></td>
<td></td>
<td>
<input type="button" id="Createuser" value="Submit" data-ng-click="createuser()" />
<input type="button" id="Clear" value="Clear" data-ng-click="Clear()" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<script src="~/RegistrationScript/Registration.js"></script>

这是我运行应用程序时的屏幕截图。

Click here to see the result

最佳答案

您的服务应如下所示:

app.service("myService", function ($http) {
//Create new record
this.post = function (User) {
var request = $http({
method: "post",
url: "http://localhost:52098/HalifaxIISService.svc/Register",
data: JSON.stringify(User)
});
return request;

};
this.getAllStudent = function () {
return $http.get("http://localhost:56766/StudentService.svc/GetAllStudent");
}

})

this.getAllStudent 应该位于 this.post 函数之外。

在 Controller 中:

var promiseGet = myService.getAllStudent();

关于javascript - Angular JS 方法未使用 Wcf 服务定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46437852/

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