The app structure:
Front-End: app.js
/** * Created by Answer1215 on 12/9/2014.*/' Use strict ';functionMainctrl (peopleservice) {varVM = This; Vm.people= []; Vm.selectedperson= {}; Vm.getpeople= Peopleservice.getpeople (). Then (function(response) {Vm.people=Response.data; }); Vm.selectperson=function(person) {Peopleservice.getselectedperson (person). Then (function(response) {Vm.selectedperson=Response.data; Vm.selectedPerson.fullName= Response.data.firstName + "" +Response.data.lastName; }); }}functionPeopleservice ($http) {varPeopleservice = {}; Peopleservice.getpeople=function() { return$http. Get (' Http://localhost:3000/people '); } Peopleservice.getselectedperson=function(person) {return$http. Get (' http://localhost:3000/people/' +person._id); } returnPeopleservice;} Angular.module (' App ',[ ' Ngresource ']). Controller (' Mainctrl ', Mainctrl). Service (' Peopleservice ', peopleservice);
<!DOCTYPE HTML><HTML><HeadLang= "en"> <MetaCharSet= "UTF-8"> <title></title> <Linkrel= "stylesheet"href= "Bower_components/bootstrap/dist/css/bootstrap.min.css"/></Head><BodyNg-app= "App"><Divclass= "Row"Ng-controller= "Mainctrl as VM"> <Divclass= "Col-xs-4"> <ul> <Ling-repeat= "person in Vm.people"Ng-click= "Vm.selectperson (person)">{{Person.firstname}}</Li> </ul> </Div> <Divclass= "Col-xs-6"> <H2>{{Vm.selectedPerson.fullName}}</H2> <span>{{Vm.selectedPerson.email}}</span> </Div></Div> <Scriptsrc= "Bower_components/angular/angular.min.js"></Script> <Scriptsrc= "Bower_components/angular-resource/angular-resource.min.js"></Script> <Scriptsrc= "App.js"></Script></Body></HTML>
Backend:
Server.js
' Use strict '; var express = require (' Express '); var cors = require ("cors"); var app = Express (); App.use (Cors ()); var people = require ('./controller/people '); App.get ('/people ', People.getall); App.get (' /people/:id ', people.get); App.listen (3000);
Controller/people.js
/** * Created by Answer1215 on 12/26/2014.*/varModel = require ('.. /model/people '); Exports.getall=function(Request, Response) {Model.getall (function(err, data) {if(ERR) Response.json (503, {error:true}); Response.json (200, data); });} Exports.get=function(Request, response) {varID =request.params.id; Model.get (ID,function(err, data) {if(ERR) Response.json (503, {error:true}); Response.json (200, data); });}
Model/people.js
/** * Created by Answer1215 on 12/26/2014.*/varMongoose = require ('.. /lib/mogoose ');varPersonschema ={firstname:string, lastname:string, email:string};//Create a person model, and rename db as Peoplevarperson = Mongoose.model (' person ', Personschema); Exports.getall=function(callback) {Person.find (). Select ("FirstName"). EXEC (function(err, data) {//Response.json (data);callback (err, data); });} Exports.get=function(ID, callback) {Person.findbyid (ID,function(err, data) {//response.send (data);callback (err, data); });}
Lib/mogoose.js
/**/var mongoose = require (' Mongoose '); Mongoose.connect (' mongodb:// Localhost/simple '= Mongoose;
[MEAN Stack] First API--2