Compare Page Revisions
« Older Revision - Back to Page History - Newer Revision »
/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; } } });
TeamPersonController.js
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; }); });
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.