I have code for the ngList directive developed by AngularJS. I would like to implement something similar to it, but I do not understand parts of the code. Here is the code
var ngListDirective = function() {
return {
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
var match = /\/(.*)\
separator = match && new RegExp(match[1]) || attr.ngList || ',';
var parse = function(viewValue) {
var list = [];
if (viewValue) {
forEach(viewValue.split(separator), function(value) {
if (value) list.push(trim(value));
});
}
return list;
};
ctrl.$parsers.push(parse);
ctrl.$formatters.push(function(value) {
if (isArray(value)) {
return value.join(', ');
}
return undefined;
});
}
};
};
Here are the parts that I do not understand:
- function (scope, element, attr, ctrl) : ctrl variable. Where can I find out more about this.
- ctrl. $ parsers.push (parse); : where can I find out more about this use
- ctrl. $ formatters.push (function (value) { : I need to understand this as I will try to implement something like this
- required: 'ngModel' : why ngModel required
source
share