【问题标题】:How to use external Javascript function in Angular 2 template?如何在 Angular 2 模板中使用外部 Javascript 函数?
【发布时间】:2016-08-22 14:24:56
【问题描述】:

我在组件中有一个带有 4 个 div 标签的模板。我要调用的JavaScript 函数changeValue() 应该将第一个div 的内容从1 更改为Yes!。我是 TypeScript 和 Angular 2 的新手,所以我不确定如何让模板与 dtest2.js 的函数进行交互:

 /**app.component.ts */

import { Component } from '@angular/core';

@Component({
selector: 'my-app',
template: `
  <div id="boolean"><p id="bv">1</p></div>
  <div id="numeric"><p id="nv">2</p></div> 
  <div id="string"><p id="sv">3</p></div> 
  <div id="enum"><p id="ev">4</p></div>
  `
})

export class AppComponent { }

//dtest2.js

function changeValue(){
  var newVal= "Yes!"; 
  document.getElementById("bv").innerHTML= newVal;
}

changeValue();
<html>
  <head>
    <title>Angular 2 QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

    <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>

    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="file:^html/angular/app/bs.min.js"></script>
    <script src="file:^html/dtest2.js"></script>
    
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <my-app>Loading...</my-app>
  </body>
</html>

【问题讨论】:

    标签: javascript html angular typescript


    【解决方案1】:

    使用这个external js function 有什么特殊原因吗? 最好使用Angular的绑定方法来解决你的问题..

    /**app.component.ts */
    
    import { Component } from '@angular/core';
    
    declare function changeValues(anyArgs: string, canBeHere: string) : void;
    
    @Component({
    selector: 'my-app',
    template: `
      <div id="boolean"><p id="bv">{{booleanValue ? 'Yes' : 'No'}}</p></div>
      <div id="numeric"><p id="nv">2</p></div> 
      <div id="string"><p id="sv">3</p></div> 
      <div id="enum"><p id="ev">4</p></div>
      `
    })
    
    export class AppComponent {
        booleanValue: boolean = true;
    
        constructor() { changeValues(...); }
    
        anyFunctionToChangeTheValue() {
            this.booleanValue = false;
        }
    }
    

    【讨论】:

    • 抱歉,我编辑了 div 的名称。忽略布尔值、字符串等。它们毫无意义。
    【解决方案2】:

    就像前面的答案所说,您可以在 Angular 2 中本地执行此操作。但是,您可以通过以下方式访问 Angular2 之外的函数。您只需在 Angular 组件中将“窗口”对象声明为常量。

    //our root app component
    import {Component, NgModule, OnInit} from '@angular/core'
    import {BrowserModule} from '@angular/platform-browser'
    
    const window: any = {};
    
    @Component({
    selector: 'my-app',
    template: `
      <div id="boolean"><p id="bv">1</p></div>
      <div id="numeric"><p id="nv">2</p></div> 
      <div id="string"><p id="sv">3</p></div> 
      <div id="enum"><p id="ev">4</p></div>
      `
    })
    export class App implements OnInit {
      ngOnInit() {
        changeValue();
      }
    }
    
    @NgModule({
      imports: [ BrowserModule ],
      declarations: [ App ],
      bootstrap: [ App ]
    })
    export class AppModule {}
    

    这是working example

    【讨论】:

    • 我在终端中收到错误:检测到文件更改。开始增量编译... [0] app/app.component.ts(6,7): error TS1155: 'const' declarations must be initialized [0] app/app.component.ts(20,5): error TS2304 : 找不到名称“changeValue”。
    • 是的,这在 plnkr 中有效,但可能不在本地,让我看看我是否可以创建一个有效的本地示例,我会在有答案时更新答案。
    • 我仍然收到 'app/app.component.ts(20,5): error TS2304: Cannot find name 'changeValue' 错误。我的其他文件是否还有其他问题?
    • 尝试用“window.changeValue()”而不是仅仅用changeValue()"调用它
    【解决方案3】:

    你可以使用:

    import { Component,ElementRef } from '@angular/core';
    constructor(private elementRef:ElementRef) {};
    
    ngAfterViewInit() {
      var s = document.createElement("script");
      s.type = "text/javascript";
      s.src = "./app/custom.js";
      this.elementRef.nativeElement.appendChild(s);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 2017-10-26
      • 2016-09-07
      相关资源
      最近更新 更多