Angular model object with JavaScript classes

0
249

Hello there,
Today I will share with you how can declare an object model in Angular with JavaScript.

var myApp = angular.module('myApp', []);

Now. We will write a factory for declaring an object model.

myApp.factory('students', function () {
//constructor
var students = function (name, age) {
this.name = name;
this.age = age;
};
//create some function
students.prototype.hi = function () {
console.info("Hello I'm : " + this.name);
};
students.prototype.age = function () {
console.info("I'm " + this.age);
};
students.prototype.run = function () {
console.info("I'm running!");
};
students.property.upAge = function () {
this.age += 1;
console.info("I'm " + this.age);
};
return students;
});

How to use it?. Follow me.

myApp.controller('StudentControllers', ['students', function (students) {
var newStudents = new students('Andy', 22);
newStudents.hi();
newStudents.age();
newStudents.run();
newStudents.upAge();
}]);

LEAVE A REPLY

Please enter your comment!
Please enter your name here