PK SsÒF”[€ÿ" ÿ" app.jsvar app = angular.module('plunker', ['ngRoute']); app .config([ "$routeProvider", function($routeProvider){ $routeProvider .when ('/', { templateUrl: "customers.html", controller : "customersController", resolve : { //This resolve thing is new here, it triggers some functions before displaying the screen. postPromise: ["customers", function(customers){ return customers.fetchAll();}] } }) .when('/customer/:id', { templateUrl: "customer.html", controller : "customerController", resolve : { customer: ["$route", "customers", function($route, customers) { //I had to consolt stack overflow for the "current" params bit. console.log($route.current.params); return customers.fetchOne($route.current.params.id); }] } }) .otherwise({ redirectTo : "/" }); } ]); app.factory('customers', ["$http", function($http){ var object = { customers : [], //Now we call the API as it was laid out in the server file. fetchAll : function(){ // object.customers = [{"CustomerId":1,"FirstName":"LuÃs","LastName":"Gonçalves","Email":"luisg@embraer.com.br"},{"CustomerId":2,"FirstName":"Leonie","LastName":"Köhler","Email":"leonekohler@surfeu.de"},{"CustomerId":3,"FirstName":"François","LastName":"Tremblay","Email":"ftremblay@gmail.com"},{"CustomerId":4,"FirstName":"Bjørn","LastName":"Hansen","Email":"bjorn.hansen@yahoo.no"},{"CustomerId":5,"FirstName":"FrantiÅ¡ek","LastName":"Wichterlová","Email":"frantisekw@jetbrains.com"},{"CustomerId":6,"FirstName":"Helena","LastName":"Holý","Email":"hholy@gmail.com"},{"CustomerId":7,"FirstName":"Astrid","LastName":"Gruber","Email":"astrid.gruber@apple.at"},{"CustomerId":8,"FirstName":"Daan","LastName":"Peeters","Email":"daan_peeters@apple.be"},{"CustomerId":9,"FirstName":"Kara","LastName":"Nielsen","Email":"kara.nielsen@jubii.dk"},{"CustomerId":10,"FirstName":"Eduardo","LastName":"Martins","Email":"eduardo@woodstock.com.br"},{"CustomerId":11,"FirstName":"Alexandre","LastName":"Rocha","Email":"alero@uol.com.br"},{"CustomerId":12,"FirstName":"Roberto","LastName":"Almeida","Email":"roberto.almeida@riotur.gov.br"},{"CustomerId":13,"FirstName":"Fernanda","LastName":"Ramos","Email":"fernadaramos4@uol.com.br"},{"CustomerId":14,"FirstName":"Mark","LastName":"Philips","Email":"mphilips12@shaw.ca"},{"CustomerId":15,"FirstName":"Jennifer","LastName":"Peterson","Email":"jenniferp@rogers.ca"},{"CustomerId":16,"FirstName":"Frank","LastName":"Harris","Email":"fharris@google.com"},{"CustomerId":17,"FirstName":"Jack","LastName":"Smith","Email":"jacksmith@microsoft.com"},{"CustomerId":18,"FirstName":"Michelle","LastName":"Brooks","Email":"michelleb@aol.com"},{"CustomerId":19,"FirstName":"Tim","LastName":"Goyer","Email":"tgoyer@apple.com"},{"CustomerId":20,"FirstName":"Dan","LastName":"Miller","Email":"dmiller@comcast.com"},{"CustomerId":21,"FirstName":"Kathy","LastName":"Chase","Email":"kachase@hotmail.com"},{"CustomerId":22,"FirstName":"Heather","LastName":"Leacock","Email":"hleacock@gmail.com"},{"CustomerId":23,"FirstName":"John","LastName":"Gordon","Email":"johngordon22@yahoo.com"},{"CustomerId":24,"FirstName":"Frank","LastName":"Ralston","Email":"fralston@gmail.com"},{"CustomerId":25,"FirstName":"Victor","LastName":"Stevens","Email":"vstevens@yahoo.com"},{"CustomerId":26,"FirstName":"Richard","LastName":"Cunningham","Email":"ricunningham@hotmail.com"},{"CustomerId":27,"FirstName":"Patrick","LastName":"Gray","Email":"patrick.gray@aol.com"},{"CustomerId":28,"FirstName":"Julia","LastName":"Barnett","Email":"jubarnett@gmail.com"},{"CustomerId":29,"FirstName":"Robert","LastName":"Brown","Email":"robbrown@shaw.ca"},{"CustomerId":30,"FirstName":"Edward","LastName":"Francis","Email":"edfrancis@yachoo.ca"},{"CustomerId":31,"FirstName":"Martha","LastName":"Silk","Email":"marthasilk@gmail.com"},{"CustomerId":32,"FirstName":"Aaron","LastName":"Mitchell","Email":"aaronmitchell@yahoo.ca"},{"CustomerId":33,"FirstName":"Ellie","LastName":"Sullivan","Email":"ellie.sullivan@shaw.ca"},{"CustomerId":34,"FirstName":"João","LastName":"Fernandes","Email":"jfernandes@yahoo.pt"},{"CustomerId":35,"FirstName":"Madalena","LastName":"Sampaio","Email":"masampaio@sapo.pt"},{"CustomerId":36,"FirstName":"Hannah","LastName":"Schneider","Email":"hannah.schneider@yahoo.de"},{"CustomerId":37,"FirstName":"Fynn","LastName":"Zimmermann","Email":"fzimmermann@yahoo.de"},{"CustomerId":38,"FirstName":"Niklas","LastName":"Schröder","Email":"nschroder@surfeu.de"},{"CustomerId":39,"FirstName":"Camille","LastName":"Bernard","Email":"camille.bernard@yahoo.fr"},{"CustomerId":40,"FirstName":"Dominique","LastName":"Lefebvre","Email":"dominiquelefebvre@gmail.com"},{"CustomerId":41,"FirstName":"Marc","LastName":"Dubois","Email":"marc.dubois@hotmail.com"},{"CustomerId":42,"FirstName":"Wyatt","LastName":"Girard","Email":"wyatt.girard@yahoo.fr"},{"CustomerId":43,"FirstName":"Isabelle","LastName":"Mercier","Email":"isabelle_mercier@apple.fr"},{"CustomerId":44,"FirstName":"Terhi","LastName":"Hämäläinen","Email":"terhi.hamalainen@apple.fi"},{"CustomerId":45,"FirstName":"Ladislav","LastName":"Kovács","Email":"ladislav_kovacs@apple.hu"},{"CustomerId":46,"FirstName":"Hugh","LastName":"O'Reilly","Email":"hughoreilly@apple.ie"},{"CustomerId":47,"FirstName":"Lucas","LastName":"Mancini","Email":"lucas.mancini@yahoo.it"},{"CustomerId":48,"FirstName":"Johannes","LastName":"Van der Berg","Email":"johavanderberg@yahoo.nl"},{"CustomerId":49,"FirstName":"StanisÅ‚aw","LastName":"Wójcik","Email":"stanisÅ‚aw.wójcik@wp.pl"},{"CustomerId":50,"FirstName":"Enrique","LastName":"Muñoz","Email":"enrique_munoz@yahoo.es"},{"CustomerId":51,"FirstName":"Joakim","LastName":"Johansson","Email":"joakim.johansson@yahoo.se"},{"CustomerId":52,"FirstName":"Emma","LastName":"Jones","Email":"emma_jones@hotmail.com"},{"CustomerId":53,"FirstName":"Phil","LastName":"Hughes","Email":"phil.hughes@gmail.com"},{"CustomerId":54,"FirstName":"Steve","LastName":"Murray","Email":"steve.murray@yahoo.uk"},{"CustomerId":55,"FirstName":"Mark","LastName":"Taylor","Email":"mark.taylor@yahoo.au"},{"CustomerId":56,"FirstName":"Diego","LastName":"Gutiérrez","Email":"diego.gutierrez@yahoo.ar"},{"CustomerId":57,"FirstName":"Luis","LastName":"Rojas","Email":"luisrojas@yahoo.cl"},{"CustomerId":58,"FirstName":"Manoj","LastName":"Pareek","Email":"manoj.pareek@rediff.com"},{"CustomerId":59,"FirstName":"Puja","LastName":"Srivastava","Email":"puja_srivastava@yahoo.in"},{"CustomerId":60,"FirstName":"Buffalo","LastName":"Bill","Email":"no"},{"CustomerId":61,"FirstName":"Buffalo","LastName":"Bill","Email":"no"},{"CustomerId":62,"FirstName":"Buff","LastName":"Bill","Email":"no"},{"CustomerId":64,"FirstName":"Bonnie","LastName":"Bonjov","Email":"bojovwomen@themailmen.com"}]; $http.get("/customer").success(function(data){ angular.copy(data, object.customers); }); }, fetchOne : function(id) { // return {"CustomerId":1,"FirstName":"LuÃs","LastName":"Gonçalves","Company":"Embraer - Empresa Brasileira de Aeronáutica S.A.","Address":"Av. Brigadeiro Faria Lima, 2170","City":"São José dos Campos","State":"SP","Country":"Brazil","PostalCode":"12227-000","Phone":"+55 (12) 3923-5555","Fax":"+55 (12) 3923-5566","Email":"luisg@embraer.com.br","SupportRepId":3}; return $http.get("/customer/"+id).then(function(res){ return res.data; }); }, createPost : function(customer) { return $http.post("/customer", customer).success(function(data){ object.customers.push(data); }); } }; return object; }]); app.controller( "customersController", ["$scope", "customers", function($scope, customers){ $scope.text = ""; $scope.customers = customers.customers; //very similar to above code. $scope.addCustomer = function(){ if (!$scope.fname || $scope.fname === ''){ return; } customers.createCustomer({FirstName: $scope.fname, LastName: $scope.lname}); $scope.fname = ''; $scope.lname = ''; }; } ] ); app.controller( "customerController", //post is the results of the resolve action when hitting a specific route, it is thus also the result of "fetchOne" ["$scope", "customers", "customer", function($scope, customers, customer){ $scope.text = ""; $scope.customer = customer; } ] ); //app.controller('MainCtrl', function($scope) { // $scope.customers = [{"CustomerId":1,"FirstName":"LuÃs","LastName":"Gonçalves","Company":"Embraer - Empresa Brasileira de Aeronáutica S.A.","Address":"Av. Brigadeiro Faria Lima, 2170","City":"São José dos Campos","State":"SP","Country":"Brazil","PostalCode":"12227-000","Phone":"+55 (12) 3923-5555","Fax":"+55 (12) 3923-5566","Email":"luisg@embraer.com.br","SupportRepId":3}]; //}); PK SsÒFuúÖU U index.html
Information about {{customer.FirstName}} {{customer.LastName}}:
Customer ID: {{customer.CustomerID}}
Company: {{customer.Company}}
Address: {{customer.Address}}
City: {{customer.City}}
State: {{customer.State}}
PostalCode: {{customer.PostalCode}}
Phone: {{customer.Phone}}
Email: {{customer.Email}}