import { Component } from '@angular/core';
import { FormGroup, FormControl, FormBuilder } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: 'app/app.template.html'
})
export class AppComponent {
public myForm: FormGroup;
public result = {};
public reactiveForm: FormGroup;
countries = ["Ireland", "New Zealand"];
constructor(private fb: FormBuilder) {
this.myForm = new FormGroup({
'testMask': new FormControl()
});
this.myForm.controls['testMask'].setValue("123");
this.result = {data: { "hello":"world"}};
this.reactiveForm = this.fb.group({
result: [this.result ]
})
}
}
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { JsonInputModule } from './json-input.module';
import { CustomInputComponent } from './custom-input.component';
// Import PrimeNG modules
import { InputMaskModule, AutoCompleteModule } from 'primeng';
@NgModule({
imports: [ BrowserModule, FormsModule, ReactiveFormsModule, InputMaskModule, JsonInputModule, AutoCompleteModule ],
declarations: [ AppComponent, CustomInputComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
body {
padding: 2em;
font-family: Arial, Helvetica, sans-serif;
}
.ui-inputwrapper-filled + label {
color: red;
}
<!DOCTYPE html>
<html>
<head>
<title>Angular QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- PrimeNG style dependencies -->
<link rel="stylesheet" href="https://unpkg.com/primeng@2.0.0/resources/themes/omega/theme.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="https://unpkg.com/primeng@2.0.0/resources/primeng.min.css" />
<!-- 1. Load libraries -->
<!-- Polyfill for older browsers -->
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.6.25?main=browser"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.8"></script>
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
/**
* WEB ANGULAR VERSION
* (based on systemjs.config.js in angular.io)
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
// Copy of compiler options in standard tsconfig.json
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
// paths serve as alias
'npm:': 'https://unpkg.com/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core@2.4.1/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common@2.4.1/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler@2.4.1/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser@2.4.1/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic@2.4.1/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http@2.4.1/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router@3.4.1/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms@2.4.1/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
'ts': 'npm:plugin-typescript@4.0.10/lib/plugin.js',
'typescript': 'npm:typescript@2.1.4/lib/typescript.js',
'primeng': 'npm:primeng@2.0.0/primeng.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
},
primeng: {
defaultExtension: 'js'
}
}
});
if (!global.noBootstrap) { bootstrap(); }
// Bootstrap the `AppModule`(skip the `app/main.ts` that normally does this)
function bootstrap() {
console.log('Auto-bootstrapping');
// Stub out `app/main.ts` so System.import('app') doesn't fail if called in the index.html
System.set(System.normalizeSync('app/main.ts'), System.newModule({ }));
// bootstrap and launch the app (equivalent to standard main.ts)
Promise.all([
System.import('@angular/platform-browser-dynamic'),
getAppModule()
])
.then(function (imports) {
var platform = imports[0];
var app = imports[1];
platform.platformBrowserDynamic().bootstrapModule(app.AppModule);
})
.catch(function(err){ console.error(err); });
}
// Import AppModule or make the default AppModule if there isn't one
// returns a promise for the AppModule
function getAppModule() {
if (global.noAppModule) {
return makeAppModule();
}
return System.import('app/app.module').catch(makeAppModule)
}
function makeAppModule() {
console.log('No AppModule; making a bare-bones, default AppModule');
return Promise.all([
System.import('@angular/core'),
System.import('@angular/platform-browser'),
System.import('app/app.component')
])
.then(function (imports) {
var core = imports[0];
var browser = imports[1];
var appComp = imports[2].AppComponent;
var AppModule = function() {}
AppModule.annotations = [
new core.NgModule({
imports: [ browser.BrowserModule ],
declarations: [ appComp ],
bootstrap: [ appComp ]
})
]
return {AppModule: AppModule};
})
}
})(this);
<h2>p-InputMask does not update its 'filled' property</h2>
<form [formGroup]="myForm">
<p-inputMask formControlName="testMask" mask="999"></p-inputMask>
<label>I should be in red after loading</label>
</form>
<h1>Reactive Form</h1>
<form [formGroup]="reactiveForm">
<json-input formControlName="result"></json-input>
</form>
<p>form is valid: {{ reactiveForm.valid ? 'true' : 'false' }}</p>
<p>Value:</p>
<pre>{{ reactiveForm.value | json }}</pre>
<!--<custom-input [(ngModel)]="test"></custom-input>-->
<custom-input [(ngModel)]="countries" ></custom-input>
<pre>Test: {{result | json}}</pre>
# PrimeNG Issue Template
Please create a test case and attach the link of the plunkr to your github issue report.
import { Component, Input, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, FormControl, Validator } from '@angular/forms';
@Component({
selector: 'json-input',
template:
`
<textarea
rows="4" cols="50"
[value]="jsonString"
(change)="onChange($event)"
(keyup)="onChange($event)">
</textarea>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => JsonInputComponent),
multi: true,
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => JsonInputComponent),
multi: true,
}]
})
export class JsonInputComponent implements ControlValueAccessor, Validator {
private jsonString: string;
private parseError: boolean;
private data: any;
// the method set in registerOnChange to emit changes back to the form
private propagateChange = (_: any) => { };
// this is the initial value set to the component
public writeValue(obj: any) {
if (obj) {
this.data = obj;
// this will format it with 4 character spacing
this.jsonString = JSON.stringify(this.data, undefined, 4);
}
}
// registers 'fn' that will be fired wheb changes are made
// this is how we emit the changes back to the form
public registerOnChange(fn: any) {
this.propagateChange = fn;
}
// validates the form, returns null when valid else the validation object
// in this case we're checking if the json parsing has passed or failed from the onChange method
public validate(c: FormControl) {
return (!this.parseError) ? null : {
jsonParseError: {
valid: false,
},
};
}
// not used, used for touch input
public registerOnTouched() { }
// change events from the textarea
private onChange(event) {
// get value from text area
let newValue = event.target.value;
try {
// parse it to json
this.data = JSON.parse(newValue);
this.parseError = false;
} catch (ex) {
// set parse error if it fails
this.parseError = true;
}
// update the form
this.propagateChange(this.data);
}
}
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { JsonInputComponent } from './json-input.component';
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
],
exports: [
JsonInputComponent,
],
declarations: [
JsonInputComponent,
],
//providers: [],
})
export class JsonInputModule { }
import { Component, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import {AutoCompleteModule} from 'primeng/primeng';
const noop = () => {
};
export const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CustomInputComponent),
multi: true
};
@Component({
selector: 'custom-input',
template: `<div class="form-group">
<p-autoComplete [multiple]="true" [(ngModel)]="value" [suggestions]="results" (completeMethod)="search($event)"></p-autoComplete>
<!--
<label><ng-content></ng-content>
<input [(ngModel)]="value"
class="form-control"
(blur)="onBlur()" >
</label>
-->
</div>`,
providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class CustomInputComponent implements ControlValueAccessor {
text: string;
results: string[] = [];
search(event) {
this.results = ["Ireland","France","Wales"];
}
//The internal data model
private innerValue: any = '';
//Placeholders for the callbacks which are later provided
//by the Control Value Accessor
private onTouchedCallback: () => void = noop;
private onChangeCallback: (_: any) => void = noop;
//get accessor
get value(): any {
return this.innerValue;
};
//set accessor including call the onchange callback
set value(v: any) {
if (v !== this.innerValue) {
this.innerValue = v;
this.onChangeCallback(v);
}
}
//Set touched on blur
onBlur() {
this.onTouchedCallback();
}
//From ControlValueAccessor interface
writeValue(value: any) {
if (value !== this.innerValue) {
this.innerValue = value;
}
}
//From ControlValueAccessor interface
registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
//From ControlValueAccessor interface
registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
}