Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.2.1/rxjs.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.5.7/core.js"></script>
<script src="https://unpkg.com/@angular/[email protected]/bundles/core.umd.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/zone.js/0.8.26/zone.min.js"></script>
<script src="https://unpkg.com/@angular/[email protected]/bundles/http.umd.js"></script>
<script src="https://unpkg.com/@angular/[email protected]/bundles/common.umd.js"></script>
<script src="https://unpkg.com/@angular/[email protected]/bundles/router.umd.js"></script>
<script src="https://unpkg.com/@angular/[email protected]/bundles/compiler.umd.js"></script>
<script src="https://unpkg.com/@angular/[email protected]/bundles/platform-browser.umd.js"></script>
<script src="https://unpkg.com/@angular/[email protected]/bundles/platform-browser-dynamic.umd.js"></script>
<script src="https://unpkg.com/@angular/[email protected]/bundles/forms.umd.js"></script>

<my-app></my-app>
              
            
!

CSS

              
                
              
            
!

JS

              
                // app.js

const { Component, VERSION, Pipe, PipeTransform } = ng.core;

@Pipe({
  name: 'applyPure',
  pure: true // immutable (value) inputs & pure fn (function)
})
class ApplyPurePipe implements PipeTransform {

  transform(value: any, fn: Function): any {
    return fn(value);
  }
}

@Pipe({
  name: 'apply',
  pure: false // any (value) inputs & any fn (function)
})
class ApplyPipe implements PipeTransform {

  transform(value: any, fn: Function): any {
    return fn(value);
  }

}

@Component({
  selector: 'my-app',
  template: `
<div>
<hr/>
<label for="value">Input {{fromScale}}
  <input id="value" [value]="value" type="number" (input)="value = $event.target.value" >
</label>

<button (click)="toggleScale()">Convert to {{toScale}}</button> | 1 kg = 2.204 lbs

<hr/>

<h3>With applyPure Pipe :(</h3>
<h4>Note: with applyPure pipe, the conversion pipe is only applied if input changes, not when only the scale toggles</h4>
<div style="font-size: x-large" [style.color]="(value > (value | applyPure: covertValue)?toScale == 'kg': toScale == 'lbs')?'green':'red'">
  {{value}} {{fromScale}} = {{value | applyPure: covertValue}} {{toScale}}
</div>
<h4>convertValue(input) is <strong>not a pure function </strong>it depends on component.toScale, which it closes over from outside (component.toScale)</h4>
<h4>If use it using <em>applyPure</em> pipe (pure = true), the pipe bindings will not be evaluated when the toScale changes on press of button as it detects input only</h4>
<hr/>

<h3>With apply Pipe :)</h3>
<h4>Note: with apply pipe, the conversion pipe is applied with standard change detection, which detects changes to both, input and the scale</h4>
<div style="font-size: x-large" [style.color]="(value > (value | apply: covertValue)?toScale == 'kg': toScale == 'lbs')?'green':'red'">
  {{value}} {{fromScale}} =  {{value | apply: covertValue}} {{toScale}}
</div>
<h4>We know convertValue(input) is <strong>not a pure function </strong> hence we are using it with apply pipe not <del>applyPure pipe</del></h4>
<h4>The <em>apply</em> pipe (pure = false), the pipe bindings will be evaluated when the toScale changes on press of button as well as on changes to input</h4>

<hr/>
</div>
  `
})
class AppComponent  {

  value = 0;
  fromScale: 'kg' | 'lbs' = 'kg';
  toScale: 'kg' | 'lbs' = 'lbs';
  toggle = false;

  constructor() {}

  toggleScale() {
    this.toggle = !this.toggle;
    this.fromScale = this.toggle ?  'lbs' : 'kg';
    this.toScale = this.toggle ? 'kg' : 'lbs' ;
  }

  covertValue = (number) => {
    return this.toScale === 'kg' ? number / 2.2 : number * 2.204;
  }
  
}

// main.js
const { BrowserModule } = ng.platformBrowser;
const { NgModule } = ng.core;
const { CommonModule } = ng.common;
const { FormsModule } =  ng.forms

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    CommonModule,
  ],
  declarations: [AppComponent, ApplyPipe, ApplyPurePipe],
  bootstrap: [AppComponent],
  providers: []
})
class AppModule {}

const { platformBrowserDynamic } = ng.platformBrowserDynamic; 

platformBrowserDynamic()
  .bootstrapModule(AppModule)
  .catch(err => console.error(err));


              
            
!
999px

Console