我想在AngularJs中创建一个Login窗体控件。但是应该从模型类(Doctor.cs)中获取用户名和密码。使用服务控制器和Web Api,如果他是经过身份验证的医生,那么他应该被重定向到 DoctorPanel.cshtml
login.cshtml
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">Email</label>
<div class="col-md-10">
<input type="email" id="email" class="form-control" ng-model="email" required />
</div>
</div>
<div class="form-group">
<label for="inputPwd" class="col-sm-2 control-label">Password</label>
<div class="col-md-10">
<input type="text" id="password" class="form-control" ng-model="password" required />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" ng-click="login()" data-ng-disabled="!formLogin.$valid">login</button>
</div>
</div>
</form>
</div>
模型类Doctor.cs
public class Doctor
{
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
DoctorsAPIController.cs
public class DoctorsAPIController : ApiController
{
private DigitalHealthWebContext db = new DigitalHealthWebContext();
// GET: api/DoctorsAPI
public List<Doctor> GetDoctors()
{
return db.Doctors.ToList();
}
//public static List<Doctor> GetDoctors()
//{
// var query = from doctor in DigitalHealthWebContext.Doctors
// select doctor;
// return db.Doctors.ToList();
//}
// GET: api/DoctorsAPI/5
[ResponseType(typeof(Doctor))]
public async Task<IHttpActionResult> GetDoctor(int id)
{
Doctor doctor = await db.Doctors.FindAsync(id);
if (doctor == null)
{
return NotFound();
}
return Ok(doctor);
}
// PUT: api/DoctorsAPI/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutDoctor(int id, Doctor doctor)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != doctor.Id)
{
return BadRequest();
}
db.Entry(doctor).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!DoctorExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/DoctorsAPI
[ResponseType(typeof(Doctor))]
public async Task<IHttpActionResult> PostDoctor(Doctor doctor)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Doctors.Add(doctor);
await db.SaveChangesAsync();
return CreatedAtRoute("DefaultApi", new { id = doctor.Id }, doctor);
}
// DELETE: api/DoctorsAPI/5
[ResponseType(typeof(Doctor))]
public async Task<IHttpActionResult> DeleteDoctor(int id)
{
Doctor doctor = await db.Doctors.FindAsync(id);
if (doctor == null)
{
return NotFound();
}
db.Doctors.Remove(doctor);
await db.SaveChangesAsync();
return Ok(doctor);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool DoctorExists(int id)
{
return db.Doctors.Count(e => e.Id == id) > 0;
}
}
我有3个javascript文件,即module.js,service.js,controller.js。
module.js var app = angular.module(&#34; docModule&#34;,[&#34; ngRoute&#34;]);
//services.js
app.service('docService', function ($http) {
this.getDoctors = function () {
return $http.get("/api/DoctorsAPI");
}
});
最后控制器来了
controller.js
app.controller('docController', function ($scope, docService) {
loadRecords();
function loadRecords() {
var promiseGet = docService.getDoctors(); //The MEthod Call from service
promiseGet.then(function (pl) { $scope.doctors = pl.data },
function (errorPl) {
$log.error('failure loading Doctor', errorPl);
});
}
});