Compare Page Revisions
« Older Revision - Back to Page History - Newer Revision »
Table of Contents [Hide/Show]
Overview Walk-Through Initial Setup Wiring Up Angular Retrieving Data Updating Data
/Scripts/MyAngularApp
app.js
var myAngularApp = angular.module("myAngularApp", ["ngResource"]). config(function ($routeProvider) { $routeProvider .when('/Team', { controller: 'TeamDashboardController', templateUrl: 'AngularTemplates/TeamDashboard.html' }) .otherwise({ redirectTo: '/Team' }); });
/Views/Shared/_Layout.cshtml
ng-app
html
angular.min.js
@{ Layout = null; } <html> <head> <script src="~/Scripts/angular.min.js"></script> <script src="~/Scripts/angular-resource.min.js"></script> <script src="/Scripts/MyAngularApp/app.js"></script> <script type="text/javascript" src="/Scripts/MyAngularApp/controllers/TeamDashboardController.js"></script> @RenderSection("head", required: false) </head> <body ng-app="myAngularApp"> <h1>My Angular App</h1> @RenderBody() </body> </html>
/Home/Index
ng-view
<div ng-view></div>
/AngularTemplates
TeamDashboard.html
<h3>This is the Team Dashboard</h3> {{ message }}
/Scripts/MyAngularApp/controllers
TeamDashboardController.js
'use strict'; myAngularApp.controller('TeamDashboardController', function ($scope) { $scope.message = "Hello world!"; });
message
AngularTraining
/Controllers/api
PersonController
public class PersonController : ApiController { [HttpGet] public List<AngularTraining.Models.Person> Index() { using (var db = new AngularTraining.Models.AngularTrainingEntities1()) { return db.People.ToList(); } } }
WebApiConfig.cs
App_Start
public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "LookupRoute", routeTemplate: "api/{controller}/{action}", defaults: new { action = "Index" } ); }
/Scripts/MyAngularApp/services
'use strict'; myAngularApp.factory('PersonService', function ($http, $q) { return { getPeople: function () { var url = '/api/person', deferred = $q.defer(); $http.get(url) .success(function (data) { deferred.resolve(data); }) .error(function (data, status, headers, config) { deferred.reject(); }); return deferred.promise; } } });
_Layout.cshtml
PersonService.js
@{ Layout = null; } <html> <head> <script src="~/Scripts/angular.min.js"></script> <script src="~/Scripts/angular-resource.min.js"></script> <script src="/Scripts/MyAngularApp/app.js"></script> <!-- Add the following line --> <script src="/Scripts/MyAngularApp/services/PersonService.js"></script> <script type="text/javascript" src="/Scripts/MyAngularApp/controllers/TeamDashboardController.js"></script> @RenderSection("head", required: false) </head> <body ng-app="myAngularApp"> <h1>My Angular App</h1> @RenderBody() </body> </html>
PersonService
'use strict'; /* Note that [PersonService] was added as an argument after [$scope] */ myAngularApp.controller('TeamDashboardController', function ($scope, PersonService) { $scope.message = "Hello world!"; /* Add this service call */ PersonService.getPeople().then(function (data) { $scope.people = data; }); });
<ul> <li ng-repeat="person in people">{{person.FirstName}} {{person.LastName}}</li> </ul>
PersonController.cs
PostPerson
[HttpPost] public AngularTraining.Models.Person PostPerson(AngularTraining.Models.Person person) { using (var db = new AngularTraining.Models.AngularTrainingEntities1()) { var d = db.People.FirstOrDefault(o => o.PersonID == person.PersonID); if (d == null) { d = new AngularTraining.Models.Person(); db.People.Add(d); } d.FirstName = person.FirstName; d.LastName = person.LastName; d.PhoneNum = person.PhoneNum; db.SaveChanges(); return d; } }
postPerson
'use strict'; myAngularApp.factory('PersonService', function ($http, $q) { return { getPeople: function () { var url = '/api/person', deferred = $q.defer(); $http.get(url) .success(function (data) { deferred.resolve(data); }) .error(function (data, status, headers, config) { deferred.reject(); }); return deferred.promise; }, /*--- Start New Code ---*/ postPerson: function (person) { var url = '/api/person/postPerson', deferred = $q.defer(); $http.post(url,person) .success(function (data) { deferred.resolve(data); }) .error(function (data, status, headers, config) { deferred.reject(); }); return deferred.promise; } /*--- End New Code ---*/ } });
savePerson
'use strict'; myAngularApp.controller('TeamDashboardController', function ($scope, PersonService) { $scope.message = "Hello world!"; PersonService.getPeople().then(function (data) { $scope.people = data; }); /*--- Start New Code ---*/ $scope.savePerson = function (person) { PersonService.postPerson(person).then(function (data) { alert('Person saved'); }); } /*--- End New Code ---*/ });
<h3>This is the Team Dashboard</h3> {{ message }} <ul> <li ng-repeat="person in people">{{person.LastName}}, {{person.FirstName}} </li> </ul> <!-- Start New Code --> <div ng-repeat="person in people" style="width:100%"> <input type="text" ng-model="person.FirstName" /> <input type="text" ng-model="person.LastName" /> <button ng-click="savePerson(person)" >Save</button> </div> <!-- End New Code -->
savePerson(person)
$scope
PersonService.postPerson(person)
TeamDashboardController
/api/person/postPerson
/Controllers/api/PersonController
person
PersonService.postPerson(person).then()
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.