How to Access codemirror text area value in Angular2 Component?

后端 未结 2 645
情书的邮戳
情书的邮戳 2021-01-24 16:13

I am trying to link codemirror with Angular 2 (TypeScript) . Right now ,I am able to display CodeEditor using a codearea custom directive ,which dynamically loads a script file

2条回答
  •  忘掉有多难
    2021-01-24 16:45

    You can use ng2-codemirror

    Install

    npm install ng2-codemirror --save

    Setup

    systemjs.config.js

    /**
     * System configuration for Angular samples
     * Adjust as necessary for your application needs.
     */
    (function (global) {
        var data = {
            paths: {
                // paths serve as alias
                'npm:': '/node_modules/'
            },
            // map tells the System loader where to look for things
            map: {
                // our app is within the app folder
                app: '/Template/js/kpxl/app',
    
                // angular bundles
                '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
                '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
                '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
                '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
                '@angular/platform-browser-dynamic':
                    'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
                '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
                '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
                '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
    
                //Add these code mirror packages
                'ng2-codemirror': 'npm:ng2-codemirror',
                'codemirror': 'npm:codemirror',
            },
            // packages tells the System loader how to load when no filename and/or no extension
            packages: {
                codemirror: {
                    main: 'lib/codemirror.js',
                    defaultExtension: 'js'
                }
            }
        };
        data.packages['ng2-codemirror'] = { main: 'lib/index.js', defaultExtension: 'js' };
        System.config(data);
    })(this);
    

    Use Sample

    Include CodeMirror css files in your page

    
    
    

    Include CodemirrorModule in your main module :

    import { CodemirrorModule } from 'ng2-codemirror';
    
    @NgModule({
      // ... 
      imports:      [
        CodemirrorModule
      ],
      // ... 
    })
    export class AppModule { }
    

    Use in your any Component.

    import { Component } from 'angular2/core';
    
    @Component({
      selector: 'sample',
      template: `
        
        
      `
    })
    export class Sample{
      constructor(){
        this.code = `// Some code...`;
      }
    }
    

提交回复
热议问题