MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular Directives using ngModelController

A simple control: rating


Let us build a simple control, a rating widget, intended to be used as:

<rating min="0" max="5" nullifier="true" ng-model="data.rating"></rating>


No fancy CSS for now; this would render as:

0 1 2 3 4 5 x

Clicking on a number selects that rating; and clicking the "x" sets the rating to null.

app.directive('rating', function() {
 function RatingController() {
 this._ngModel = null;
 this.rating = null;
 this.options = null;
 this.min = typeof this.min === 'number' ? this.min : 1;
 this.max = typeof this.max === 'number' ? this.max : 5;
 }
 
 RatingController.prototype.setNgModel = function(ngModel) {
 this._ngModel = ngModel;
 
 if( ngModel ) {
 // KEY POINT 1
 ngModel.$render = this._render.bind(this);
 }
 };
 
 RatingController.prototype._render = function() {
 this.rating = this._ngModel.$viewValue != null ? this._ngModel.$viewValue : -
Number.MAX_VALUE;
 };
 
 RatingController.prototype._calculateOptions = function() {
 if( this.min == null || this.max == null ) {
 this.options = [];
 }
 else {
 this.options = new Array(this.max - this.min + 1);
 for( var i=0; i < this.options.length; i++ ) {
 this.options[i] = this.min + i;
 }
 }
 };
 
 RatingController.prototype.setValue = function(val) {
 this.rating = val;
 // KEY POINT 2
 this._ngModel.$setViewValue(val);
 };
 
 // KEY POINT 3
 Object.defineProperty(RatingController.prototype, 'min', {
 get: function() {
 return this._min;
 },
 set: function(val) {
 this._min = val;
 this._calculateOptions();
 }
 });
 
 Object.defineProperty(RatingController.prototype, 'max', {
 get: function() {
 return this._max;
 },
 set: function(val) {
 this._max = val;
 this._calculateOptions();
 }
 });
 
 return {
 restrict: 'E',
 scope: {
 // KEY POINT 3
 min: '<?',
 max: '<?',
 nullifier: '<?'
 },
 bindToController: true,
 controllerAs: 'ctrl',
 controller: RatingController,
 require: ['rating', 'ngModel'],
 link: function(scope, elem, attrs, ctrls) {
 ctrls[0].setNgModel(ctrls[1]);
 },
 template:
 '<span ng-repeat="o in ctrl.options" href="#" class="rating-option" ngclass="{\'rating-option-active\': o <= ctrl.rating}" ng-click="ctrl.setValue(o)">{{ o }}</span>' +
 '<span ng-if="ctrl.nullifier" ng-click="ctrl.setValue(null)" class="ratingnullifier">✖</span>'
 };
});

Key points:


1. Implement ngModel.$render to transfer the model's view value to your view.
2. Call ngModel.$setViewValue() whenever you feel the view value should be updated.
3. The control can of course be parameterized; use '<' scope bindings for parameters, if in Angular >= 1.5 to clearly indicate input - one way binding. If you have to take action whenever a parameter changes, you can use a JavaScript property (see Object.defineProperty()) to save a few watches.

Note 1: In order not to overcomplicate the implementation, the rating values are inserted in an array - the ctrl.options. This is not needed; a more efficient, but also more complex, implementation could use DOM manipulation to insert/remove ratings when min/max change.

Note 2: With the exception of the '<' scope bindings, this example can be used in Angular < 1.5. If you are on Angular >= 1.5, it would be a good idea to transform this to a component and use the $onInit() lifecycle hook to initialize min and max, instead of doing so in the controller's constructor.

Conclusion

In this page (written and validated by ) you learned about AngularJS Directives using ngModelController . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS Controllers.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.