You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.9 KiB
2.9 KiB
MEAN Stack
Lesson Objectives
- Describe the elements of the MEAN stack
- Set up static files
- Set up Angular
- Create App/Controller
- Create new todo form
- Hook form up to controller properties
- Make form make AJAX request on submit
Describe the elements of the MEAN stack
MEAN stack is just a collection of tech that work well together
- Mongo
- Express
- Angular
- Node.js
Set up static files
mkdir publictouch public/index.htmlmkdir public/jstouch public/js/app.js
server.js:
app.use(express.static('public'));
public/index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
</html>
Set up Angular
- Include Angular
- Set up app
- Test {{}}
public/index.html:
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js" charset="utf-8"></script>
</head>
<body>
{{2+2}}
</body>
</html>
Create App/Controller
public/js/app.js:
const app = angular.module('MyApp', []);
app.controller('MyController', function(){
this.foo = 'bar';
});
- ng-app
- script tag for /js/app.js
- ng-controller
- {{ctrl.foo}}
public/index.html:
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js" charset="utf-8"></script>
<script src="/js/app.js" charset="utf-8"></script>
</head>
<body ng-controller="MyController as ctrl">
{{ctrl.foo}}
</body>
</html>
Create new todo form
public/index.html:
<form>
<input type="text" placeholder="description"/>
Complete: <input type="checkbox"/>
<input type="submit" value="Create New To Do Item"/>
</form>
Hook form up to controller properties
public/index.html:
<form>
{{ctrl.description}} - {{ctrl.complete}}
<input type="text" ng-model="ctrl.description" placeholder="description"/>
Complete: <input type="checkbox" ng-model="ctrl.complete"/>
<input type="submit" value="Create New Todo Item"/>
</form>
Make form make AJAX request on submit
public/js/app.js:
app.controller('MyController', ['$http', function($http){
this.description = null;
this.complete = false;
this.createTodo = function(){
$http({
method:'POST',
url: '/todos',
data: {
description: this.description,
complete: this.complete
}
}).then(function(response){
console.log(response);
}, function(){
console.log('error');
});
}
}]);
public/index.html
<form ng-submit="ctrl.createTodo()">