angular2-ngmodel

Angular 2 set and bind checkboxes with a ngFor

柔情痞子 提交于 2019-12-01 00:53:02
I have an array like this: objectArray = [ {"name": "Car"}, {"name": "Bike"}, {"name": "Boat"}, {"name": "Plane"} ]; And the template like this: <li *ngFor="#obj of objectArray"> <a href="#" class="small" data-value="option1" tabIndex="-1"> <input type="checkbox" (change)="expression && expression.value = $event.target.checked ? true : undefind" [ngModel]="expression?.value"> <label for="">{{ obj.name }}</label> </a> </li> But this is set true when 1 checkbox is checked. How do i set this separately ? I guess this is what you want: <li *ngFor="let obj of objectArray"> <a href="#" class="small"

How to set default selected values in multiselect with ngModel in Angular 2

╄→гoц情女王★ 提交于 2019-11-30 16:12:37
问题 How to set default selected values in multiselect. I get current_options and all_options from database and I want to update current_options and send new values do database again. Updating database works but when I refresh page none of options are selected. current_options = [{id:1, name:'name1'}]; #from database all_options = [{id:1, name:'name1'},{id:2, name:'name2'}]; #from database My template: <select multiple name="type" [(ngModel)]="current_options"> <option *ngFor="let option of all

Angular - There is no directive with “exportAs” set to “ngModel”

送分小仙女□ 提交于 2019-11-29 17:51:59
问题 Following are the files in the AngularJS project. As suggested in some posts, I have added: ngModel name="currentPassword" #currentPassword="ngModel in the input field, but still getting an error. package.json ......... "dependencies": { "@angular/common": "^2.3.1", "@angular/compiler": "^2.3.1", "@angular/core": "^2.3.1", "@angular/forms": "^2.3.1", "@angular/http": "^2.3.1", "@angular/platform-browser": "^2.3.1", "@angular/platform-browser-dynamic": "^2.3.1", "@angular/router": "^3.3.1",

Angular 2: Using pipes with ngModel

牧云@^-^@ 提交于 2019-11-29 04:19:19
I was using JQuery inputmask in one of my forms along with [(ngModel)] , but for some reason they won't work together. Using either one by itself works perfectly fine, but combining the two completely breaks [(ngModel)] and new inputs don't get sent back to the component. After struggling with this for a while I figured using Angular 2's pipes would be a good solution, however I can't figure out how to get those two to work together either. Here is some code I am using for testing my pipe <input [(ngModel)]="Amount" id="Amount" name="Amount" class="form-control" type="number" autocomplete="off

Attribute directive with ngModel to change field value

大兔子大兔子 提交于 2019-11-28 17:05:48
I want to change (force) input field values while typing using a attribute Directive. With it I would like to create directives like uppercase, lowercase, maxlength, filterchar, etc. to be used on input fields on forms. I found this example: Angular 2 Attribute Directive Typescript Example but this doesn't seem to work. Maybe it did for an earlier build of Angular2. It is however exactly what I would like to do. When I create a directive like this: import {Directive} from 'angular2/core'; import {NgModel} from 'angular2/common'; @Directive({ selector: '[ngModel][uppercase]', host: { '(input)'

How to show placeholder (empty option) in select control in Angular 2?

∥☆過路亽.° 提交于 2019-11-28 05:25:00
I have this code in my template: <select [ngModel]="selectedSubSectionId" (ngModelChange)="onSubSectionChange($event)"> <option *ngFor="let subSection of event.subSections" [ngValue]="subSection.id">{{ subSection.name }}</option> </select> In my component: public selectedSubSectionId: any; public onSubSectionChange(subSectionId: any) { // some code I execute after ngModel changes. } This works ok, but at the beginning I have an empty box. I want to show a placeholder message there. How can I do this using ngModel? My solution: In the component typescript file I add a property

Angular 2 set and bind checkboxes with a ngFor

南笙酒味 提交于 2019-11-28 02:03:12
问题 I have an array like this: objectArray = [ {"name": "Car"}, {"name": "Bike"}, {"name": "Boat"}, {"name": "Plane"} ]; And the template like this: <li *ngFor="#obj of objectArray"> <a href="#" class="small" data-value="option1" tabIndex="-1"> <input type="checkbox" (change)="expression && expression.value = $event.target.checked ? true : undefind" [ngModel]="expression?.value"> <label for="">{{ obj.name }}</label> </a> </li> But this is set true when 1 checkbox is checked. How do i set this

Attribute directive with ngModel to change field value

做~自己de王妃 提交于 2019-11-27 20:02:19
问题 I want to change (force) input field values while typing using a attribute Directive. With it I would like to create directives like uppercase, lowercase, maxlength, filterchar, etc. to be used on input fields on forms. I found this example: Angular 2 Attribute Directive Typescript Example but this doesn't seem to work. Maybe it did for an earlier build of Angular2. It is however exactly what I would like to do. When I create a directive like this: import {Directive} from 'angular2/core';

Angular 2 ngModel in child component updates parent component property

半腔热情 提交于 2019-11-27 11:01:45
I made a simple UI which consist two components (parent and child). What the UI does is that when I type some stuff in the input box of the Child component. The value will change using ngModel. The child component works fine that way. // Child Component @Component({ selector: 'child', template: ` <p>{{sharedVar}}</p> <input [(ngModel)]="sharedVar"> ` }) export class ChildComponent { sharedVar: string; } Now I have a parent component which I intend to use the same value as Child Component. I added the Child Component into the Parent template, and use dependency injection to call Child Component

angular2 pass ngModel to a child component

余生颓废 提交于 2019-11-27 05:58:37
问题 I have ParentComponent and a ChildComponent, and I need to pass the ngModel in ParentComponent to ChildComponent. // the below is in ParentComponent template <child-component [(ngModel)]="valueInParentComponent"></child-component> how can I get the value of ngModel in ChildComponent and manipulate it? 回答1: You need to implement ControlValueAccessor in the child class. It's what defines your component as "having a value" that can be bound to using the angular way. Read more about it here: http