【问题标题】:Google code Prettify code in Angular2 partially working谷歌代码 Angular2 中的美化代码部分工作
【发布时间】:2017-04-09 17:39:20
【问题描述】:

我正在尝试在 html 中显示代码并使用 Google Code prettify 对其进行美化。我几乎完成了我的要求,但是当我尝试将文件外部化并从中提取代码时,它不起作用。

这是我的 ts 代码 sn-p。

demoJavaCode: any;
demoJavaCodeFromFile: any;

ngOnInit() {
    this.demoJavaCode = 
      `<pre><xmp class="prettyprint">
         public class A {
             public static void main(String args[]) {
             }
         }
        </xmp></pre>`;
 }

ngAfterViewInit() { 
  PR.prettyPrint();
}`

在模板中,我是这样获取的。

<p  [innerHtml] ="demoJavaCode | trustedHtml"></p>

它运作良好,其中包含代码的段落只有在使用trustedHTML管道进行清理时才会突出显示/美化。

但是当我试图将代码外部化到具有完全相同代码内容的外部文件时,它不起作用。

这是我的 ts sn-p。

this._http.get("assets/java_code.txt").map(res => res.text()).subscribe(
      response => {
        this.demoJavaCodeFromFile = response;
      },
      error => {
        this.componentErrorMessage = error;
      },
      () => {
        console.log('File successfully loaded..');
      }
    );

这里有什么问题?任何指示和建议都会有所帮助。

【问题讨论】:

  • 我假设 'PR.prettyPrint()' 调用一个全局 API 来使代码更漂亮?如果你在赋值this.demoJavaCodeFromFile之后调用,在subscribe的成功函数中?
  • @AhmedMusallam,确实如此,并且它希望将代码附加到文档,除非显式传入根,并且它不会进入 shadowRoots 或模板节点。见declaration

标签: angular google-code-prettify


【解决方案1】:

您应该在ngAfterViewChecked 组件lifecycle hook 内调用PR.prettyPrint();

看看这个plnkr:https://plnkr.co/edit/wDJCKx3RjpJID2Nb6j2L?p=preview

这是来自 plnkr 的代码:

//src/app.ts 
import {Component, NgModule, VERSION, AfterViewChecked} from '@angular/core'
import { FormsModule }   from '@angular/forms';
import {BrowserModule} from '@angular/platform-browser'
import { HttpModule } from '@angular/http';
import { Http } from '@angular/http';

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <button (click)="refresh()">refresh</button>
      <div [innerHtml]="code"></div>
    </div>
  `,
})
export class App implements AfterViewChecked {
  name:string;
  code: string;
  constructor(private http: Http) {
    this.name = `Angular! v${VERSION.full}`;
  }
  refresh(){
    this.http.get("javacode.html")
    .subscribe(
      res => {
        this.code = res._body;
      },
      ()=>{},
      ()=>{})

  }
  ngAfterViewChecked(){
      console.log('ngAfterViewChecked')
      PR.prettyPrint();
  }
}

@NgModule({
  imports: [ BrowserModule, HttpModule, FormsModule],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}
//--------------------------------------
//src/javacode.html
<pre class="prettyprint">
  public class Cube {

    int length;
    int breadth;
    int height;
    public int getVolume() {
        return (length * breadth * height);
    }
}
</pre>

【讨论】:

  • 如何导入 PR?我得到“找不到名称'PR'”
  • 你不能“导入”它,因为它不是一个模块,你可以像declare var PR;这样将PR声明为var,或者你可以从像var PR = window["PR"]这样的窗口命名空间中使用它
  • @ahmed-musallam 感谢您的解决方案。你知道为什么它在 ngAfterViewInit 中不起作用吗?将代码放到ngAfterViewChecked 这不是一个好主意,因为每次视图发生变化时它都会触发,如果您使用OnPush 策略则发生事件
  • @ArtemGalas 这个答案与 Angular 2 相关,我不确定在 Angular 2 之后情况是否发生了变化。重点是,您想在 DOM 渲染后调用 PR.prettyPrint(); 并确保在您致电 PR.prettyPrint(); 后,您的整个模板并未更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
相关资源
最近更新 更多