我有一个HTML INPUT字段.
<input
[(ngModel)]="item.value"
name="inputField"
type="text"
/>
Run Code Online (Sandbox Code Playgroud)
我想格式化它的值并使用现有的管道:
....
[(ngModel)]="item.value | useMyPipeToFormatThatValue"
....
Run Code Online (Sandbox Code Playgroud)
并收到错误消息:
动作表达式中不能有管道
在这种情况下如何使用管道?
我做了一个简单的UI,它包含两个组件(父和子).
UI的作用是当我在Child组件的输入框中键入一些内容时.该值将使用ngModel更改.
子组件以这种方式工作正常.
// Child Component
@Component({
selector: 'child',
template: `
<p>{{sharedVar}}</p>
<input [(ngModel)]="sharedVar">
`
})
export class ChildComponent {
sharedVar: string;
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个父组件,我打算使用与Child Component相同的值.
我将Child Component添加到Parent模板中,并使用依赖注入来调用Child Component sharedVar.
// Parent Component
@Component({
selector: 'parent',
template: `
<h1>{{sharedVar}}</h1>
<child></child>
`,
directives: [ChildComponent],
providers: [ChildCompnent]
})
export class ParentComponent {
sharedVar: string;
constructor(child: ChildComponent) {
this.sharedVar = child.sharedVar;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我在输入框中输入时,值<p>会自动更改,而父组件中的值<h1>不会更改.
我正在制作一个动态的表格.A Field有一个值列表.每个值都由一个字符串表示.
export class Field{
name: string;
values: string[] = [];
fieldType: string;
constructor(fieldType: string) {this.fieldType = fieldType;}
}
Run Code Online (Sandbox Code Playgroud)
我的组件中有一个函数,它为字段添加了一个新值.
addValue(field){
field.values.push("");
}
Run Code Online (Sandbox Code Playgroud)
值和按钮在我的HTML中显示如下.
<div id="dropdown-values" *ngFor="let value of field.values; let j=index">
<input type="text" class="form-control" [(ngModel)]="field.values[j]" [name]="'value' + j + '.' + i"/><br/>
</div>
<div class="text-center">
<a href="javascript:void(0);" (click)="addValue(field)"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>
</div>
Run Code Online (Sandbox Code Playgroud)
只要在输入值中写入一些文本,输入就会失去焦点.如果我向字段添加许多值,并且我在一个值输入中写入一个字符,则输入将失去焦点,并且字符将写入每个输入.
有没有办法在ngModelChange上获取字段的上一个(最后一个)值?我的东西是这样的
HTML
<input type="text" [(ngModel)]="text" (ngModelChange)="textChanged($event)">
Run Code Online (Sandbox Code Playgroud)
处理器
private textChanged(event) {
console.log('changed', this.text, event);
}
Run Code Online (Sandbox Code Playgroud)
我得到的是
changed *newvalue* *newvalue*
Run Code Online (Sandbox Code Playgroud)
当然我可以使用另一个变量保留旧值,但有更好的方法吗?
我在我的模板中有这个代码:
<select [ngModel]="selectedSubSectionId" (ngModelChange)="onSubSectionChange($event)">
<option *ngFor="let subSection of event.subSections" [ngValue]="subSection.id">{{ subSection.name }}</option>
</select>
Run Code Online (Sandbox Code Playgroud)
在我的组件中:
public selectedSubSectionId: any;
public onSubSectionChange(subSectionId: any) {
// some code I execute after ngModel changes.
}
Run Code Online (Sandbox Code Playgroud)
这样可以,但最初我有一个空盒子.我想在那里显示占位符消息.如何使用ngModel执行此操作?
我想在使用属性Directive键入时更改(强制)输入字段值.有了它,我想创建一些指令,如大写,小写,maxlength,filterchar等,用于表单上的输入字段.我找到了这个例子:Angular 2 Attribute Directive Typescript Example但这似乎不起作用.也许它是为早期构建的Angular2做的.然而,这正是我想要做的.
当我创建这样的指令时:
import {Directive} from 'angular2/core';
import {NgModel} from 'angular2/common';
@Directive({
selector: '[ngModel][uppercase]',
host: {
'(input)' : 'onInputChange()'
}
})
export class UppercaseDirective{
constructor(public model:NgModel){}
onInputChange(){
var newValue = this.model.value.toUpperCase();
this.model.valueAccessor.writeValue(newValue);
this.model.viewToModelUpdate(newValue);
}
}
Run Code Online (Sandbox Code Playgroud)
并在这样的表单上使用它:
<input type="text" class="form-control" [(ngModel)]="field.name" ngControl="name" #name="ngForm" required uppercase>
Run Code Online (Sandbox Code Playgroud)
(并注册NgModel为提供者).我得到了
undefined this.model.value.
我可以使用$event.target.value = $event.target.value.toUpperCase()(当传递$event时onInputChange())并且适用于视图(它确实将输入显示为大写.但它不会更新绑定字段"field.name".
那么如何创建一个Angular2属性指令呢?
- 编辑 -
经过一番进一步调查后,我设法得到了我想要的东西.Günter提供的答案更接近我的初衷,也许更好.但这是另一种方式:
import {Directive, Input, Output, EventEmitter} from 'angular2/core';
@Directive({
selector: '[ngModel][uppercase]',
host: …Run Code Online (Sandbox Code Playgroud) 我有一张表格正在填写add client表格.它工作正常,并显示更改.问题是我有一个选择列表,用户在其中选择特定的源,然后将其保存在ngmodel中.这是代码.
选择列表
<mat-select [(ngModel)]="model.source" name="source" placeholder="Select Source ">
<mat-option [value]="1 ">Upwork</mat-option>
<mat-option [value]="2 ">Refer from Friend</mat-option>
<mat-option [value]="3 ">Other</mat-option>
</mat-select>
Run Code Online (Sandbox Code Playgroud)
现在在我的表中,显示的值基于选择显示为1或2或3.我想写一些东西,这样的插值条件.
表场
<ng-container matColumnDef="source">
<mat-header-cell *matHeaderCellDef> Source </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.source ? if value is 1 : display (upwork), if value is 2 : display(refer from friend)}} </mat-cell>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
有点像这样,我确实喜欢它,angularjs我不确定Angular
我正在尝试在Angular2中进行最简单的双向绑定.我想在我的组件和它的模板之间共享一个变量.
我的模板是:
<textarea [(ngModel)]="currentQuery"></textarea>
Run Code Online (Sandbox Code Playgroud)
我的组件是:
import { Component } from '@angular/core';
import { ViewChild } from '@angular/core';
import { OnInit } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'vs-home',
templateUrl: 'home.component.html'
})
export class HomeComponent {
private currentQuery: string = '';
}
Run Code Online (Sandbox Code Playgroud)
根据文档,这应该工作,但我得到:
Error: Uncaught (in promise): Error: Template parse errors:
Can't bind to 'ngModel' since it isn't a known property of 'textarea'. ("
<div class="query-bar-container">
<textarea [ERROR ->][(ngModel)]="currentQuery"></textarea>
<!-- <button type="button" class="btn btn-default" (click"): HomeComponent@2:15
Run Code Online (Sandbox Code Playgroud) 在角度为2的ngModel中遇到问题.有一项任务是从组件类中的数组输出几个输入.发现了一种可能使ngModel从[name]属性中获取其值而不包含[()]中的ngModel.我想知道是否有办法为这些输入提供默认值.
personal-details.component.ts:
import {
Component,
} from '@angular/core';
import { Input }from './Input'
@Component({
selector: 'personal-details',
styleUrls: ['personal-details.component.sass'],
templateUrl: 'personal-details.component.html'
})
export class PersonalDetailsComponent {
title: string;
inputs: Input[] = [
new Input('Name', 'text', 'Name', true, true),
new Input('Surname', 'text', 'Surname', true, true),
new Input('Mobile phone Number', 'text', 'phone', true, true),
new Input('Date of Birth', 'text', 'birthday', false, true),
new Input('Title', 'text', 'title', false, false),
new Input('Title after name', 'text', 'title-after-name', false, false),
new Input('Personal number', 'text', 'personal-number', false, false), …Run Code Online (Sandbox Code Playgroud) 以下是AngularJS项目中的文件.正如一些帖子所建议的那样,我补充说:
ngModel name="currentPassword" #currentPassword="ngModel
Run Code Online (Sandbox Code Playgroud)
在输入字段中,但仍然出错.
的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",
"core-js": "^2.4.1",
"rxjs": "^5.0.1",
"ts-helpers": "^1.1.1",
"zone.js": "^0.7.2"
},
...............
Run Code Online (Sandbox Code Playgroud)
变化password.component.html
<form #f="ngForm" [formGroup]="changePasswordForm">
<div class="form-group">
<label for="currentPassword">Current Password</label> <input
placeholder="currentPassword" ngModel name="currentPassword"
#currentPassword="ngModel" id="currentPassword"
name="currentPassword" type="text" class="form-control" required
minlength="6" formControlName="currentPassword">
</div>
</div>
<button class="btn btn-primary" type="submit">Change Password</button>
</form>
Run Code Online (Sandbox Code Playgroud)
变化password.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators, ControlContainer, FormGroup, FormControl } …Run Code Online (Sandbox Code Playgroud) angular ×10
angular2-ngmodel ×10
javascript ×2
typescript ×2
directive ×1
html-input ×1
ngfor ×1
ngmodel ×1
pipe ×1