【问题标题】:Call a external function injected in Angular2调用Angular2中注入的外部函数
【发布时间】:2016-05-27 16:14:33
【问题描述】:

我正在寻找一种在 angular 的主要组件中执行外部函数的方法,如下所示:

<head>  
  <script>
      System.import('app').catch(function(err){ console.error(err); });

      function callback(datos){
        alert("datos: "+datos);
      }
    </script>
  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app [cb]="callback">loading...</my-app>
  </body>

并像这样在组件中调用它:

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

@Component({
  selector: 'my-app',
  template: '<h1 (click)="cb()">My First Angular 2 App</h1>'
})
export class AppComponent {
  @Input() cb;
}

但它不起作用。有人知道这样做的方法吗?

提前致谢。

【问题讨论】:

    标签: javascript angular angular2-directives


    【解决方案1】:

    您不能在根组件上使用绑定[xxx](或(xxx))。这仅适用于 Angular 组件。

    也许https://stackoverflow.com/a/36997723/217408 或者最后链接的答案可能会给你一些想法。

    【讨论】:

    • 那么,如果我不能使用绑定,如何在组件中调用外部函数?
    • 例如,您可以将函数分配给window,然后像&lt;my-app cb="callback"&gt;loading...&lt;/my-app&gt; 这样传递名称,然后像stackoverflow.com/a/36723603/217408 中解释的那样在Angular 中获取名称,然后像window[cb]() 一样调用它跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2018-06-12
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多