Angular Js Note
Angular Js Note
<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body>
</body>
</html>
my-angular-app/
├── lib/
│ └── angular.min.js
├── app/
│ ├── app.js
│ ├── controllers/
│ ├── services/
│ └── directives/
├── views/
└── index.html
<!DOCTYPE html>
<html ng-app>
<head>
<script src="lib/angular.min.js"></script>
</head>
<body>
</body>
</html>
// app/app.js
• Use a local web server (not just opening the file directly)
• Options:
Slide 6: Directives
• Simple evaluation: {{ 5 + 5 }}
<div ng-controller="UserController">
</div>
Slide 8: Controllers
app.controller('StudentController', function($scope) {
$scope.students = [
];
$scope.addStudent = function() {
$scope.students.push({
name: $scope.newName,
grade: $scope.newGrade
});
$scope.newName = '';
$scope.newGrade = '';
};
});
Slide 9: Modules
// Define a module
var app = angular.module('myApp', ['ngRoute']);
app.config(function($routeProvider) {
// Module configuration
});
app.controller('MyController', function() {
// Controller logic
});
app.service('StudentService', function($http) {
this.getAllStudents = function() {
return $http.get('/api/students');
};
this.addStudent = function(student) {
};
});
Slide 11: Using Services in Controllers
// Load students
StudentService.getAllStudents()
.then(function(response) {
$scope.students = response.data;
})
.catch(function(error) {
});
// Add a student
$scope.addStudent = function() {
var newStudent = {
name: $scope.newName,
grade: $scope.newGrade
};
StudentService.addStudent(newStudent)
.then(function(response) {
$scope.students.push(response.data);
$scope.newName = '';
$scope.newGrade = '';
});
};
});
<div ng-controller="ProductCtrl">
<ul>
{{ item.name }}
</li>
</ul>
</div>
app.filter('capitalize', function() {
return function(input) {
};
});
<div ng-controller="NameCtrl">
</div>
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html',
controller: 'HomeCtrl'
})
.when('/students', {
templateUrl: 'views/students.html',
controller: 'StudentCtrl'
})
.when('/courses', {
templateUrl: 'views/courses.html',
controller: 'CourseCtrl'
})
.otherwise({
redirectTo: '/'
});
});
Slide 15: Forms and Validation
<div>
<label>Name:</label>
Name is required
</span>
</div>
<div>
<label>Email:</label>
Email is required
</span>
</span>
</div>
</form>
app.directive('studentCard', function() {
return {
templateUrl: 'templates/student-card.html',
},
controller: function($scope) {
$scope.incrementGrade = function() {
$scope.student.grade += 1;
};
};
});
<student-card
student="currentStudent"
on-delete="removeStudent(currentStudent)">
</student-card>
o Value
o Factory
o Service
o Provider
o Constant
// Defining a service
app.service('Logger', function() {
this.log = function(msg) {
console.log(msg);
};
});
$scope.doSomething = function() {
Logger.log('Something happened!');
};
});
• Similarities
o Component-based architecture
o Data binding
o Dependency injection
o Routing
• Differences
o Missing dependencies
Requirements:
• AngularJS on GitHub