Angular2: use [(ngModel)] with [ngModelOptions]=”{standalone: true}” to link to a reference to model’s property

Using @angular/forms when you use a <form> tag it automatically creates a FormGroup. For every contained ngModel tagged <input> it will create a FormControl and add it into the FormGroup created above; this FormControl will be named into the FormGroup using attribute name. Example: <form #f=”ngForm”> <input type=”text” [(ngModel)]=”firstFieldVariable” name=”firstField”> <span>{{ f.controls[‘firstField’]?.value }}</span> </form> Said … Read more

Contenteditable with ng-model doesn’t work

contenteditable tag will not work directly with angular’s ng-model because the way contenteditable rerender the dom element on every change. You have to wrap it with a custom directive for that: JS: angular.module(‘customControl’, [‘ngSanitize’]). directive(‘contenteditable’, [‘$sce’, function($sce) { return { restrict: ‘A’, // only activate on element attribute require: ‘?ngModel’, // get a hold of … Read more

Angular bootstrap datepicker date format does not format ng-model value

Although similar answers have been posted I’d like to contribute what seemed to be the easiest and cleanest fix to me. Assuming you are using the AngularUI datepicker and your initial value for the ng-Model does not get formatted simply adding the following directive to your project will fix the issue: angular.module(‘yourAppName’) .directive(‘datepickerPopup’, function (){ … Read more

Using ng-repeat to generate select options (with Demo)

This is NOT the correct way to use ng-model with a <select> element in AngularJS: <!– ERRONEOUS <select ng-model=”vm.areas”> <option ng-repeat=”area in vm.areas” ng-value=”area” id=”{{area}}” ng-click=”send_index($event, $index)”>{{area.name}} </select> <input value=”{{variable}}”> –> There is no need to use the ng-click directive. The select directive handles that automatically. The ng-model directive receives or sets the chosen option. … Read more

Ng-model does not update controller value

“If you use ng-model, you have to have a dot in there.” Make your model point to an object.property and you’ll be good to go. Controller $scope.formData = {}; $scope.check = function () { console.log($scope.formData.searchText.$modelValue); //works } Template <input ng-model=”formData.searchText”/> <button ng-click=”check()”>Check!</button> This happens when child scopes are in play – like child routes or … Read more

ngModel Formatters and Parsers

This topic was covered really well in a related question: How to do two-way filtering in AngularJS? To summarize: Formatters change how model values will appear in the view. Parsers change how view values will be saved in the model. Here is a simple example, building on an example in the NgModelController api documentation: //format … Read more

How to set a selected option of a dropdown list control using angular JS

I hope I understand your question, but the ng-model directive creates a two-way binding between the selected item in the control and the value of item.selectedVariant. This means that changing item.selectedVariant in JavaScript, or changing the value in the control, updates the other. If item.selectedVariant has a value of 0, that item should get selected. … Read more