相关疑难解决方法(0)

Angular2 - 如何从应用程序外部调用组件功能

我正在使用具有回调的javascript对象.我希望一旦触发回调来调用Angular2组件内的函数.

示例HTML文件.

    var run = new Hello('callbackfunction');

    function callbackfunction(){   
     // how to call the function **runThisFunctionFromOutside**
   }
   <script>
      System.config({
        transpiler: 'typescript', 
        typescriptOptions: { emitDecoratorMetadata: true }, 
        packages: {'js/app': {defaultExtension: 'ts'}} 
      });
      System.import('js/app/main')
            .then(null, console.error.bind(console));
    </script>
Run Code Online (Sandbox Code Playgroud)

我的App.component.ts

import {Component NgZone} from 'angular2/core';
import {GameButtonsComponent} from './buttons/game-buttons.component';
@Component({
  selector: 'my-app',
  template: ' blblb'
})
export class AppComponent {

constructor(private _ngZone: NgZone){}

ngOnInit(){
    calledFromOutside() {
        this._ngZone.run(() => {
          this.runThisFunctionFromOutside();
    });
  }
  }
runThisFunctionFromOutside(){
   console.log("run");
}
Run Code Online (Sandbox Code Playgroud)

如何调用App.component.ts中的函数runThisFunctionFromOutside

angular

66
推荐指数
3
解决办法
8万
查看次数

使用Systemjs时检测是否在页面上加载了Angular2

我试图找出一个好的(最好是最好的)方法来检测页面上是否加载了angular2.

我看了一下这个问题:检测AngularJS是否加载到当前页面的方法,但答案只是说明要使用window.angular,这是我尝试的第一件事(很久才找到答案.)好像是systemjs导致角度不在全局(窗口)范围内.

这是我的html文件的头部:

<script src="client/node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="client/node_modules/systemjs/dist/system.src.js"></script>
<script src="client/node_modules/rxjs/bundles/Rx.js"></script>
<script src="client/node_modules/angular2/bundles/angular2.dev.js"></script>
<link rel="stylesheet" href="client/styles/main.css"/>

<script>
  System.config({
    packages: {        
        client: {
            format: 'register',
            defaultExtension: 'js'
        }
    }
  });
  System.import('client/app')
        .then(null, console.error.bind(console));
</script>
Run Code Online (Sandbox Code Playgroud)

基于这个问题,以及我对http2的理解,我没有看到不使用systemjs的真正原因.

显然,我可能是错的,systemjs可能不是问题的原因; 无论如何,window.angular即使我正在看我的角度应用程序,也会返回undefined.

为了给你我们的用例,万一它有帮助,我们正在通过一个init文件加载一个角度应用程序,该文件将在其他网站上使用(在大多数情况下我们不控制页面.)我们希望我们的init文件是能够检测页面中是否已包含angular2(和正确版本)以及其他依赖项.如果不是,我们会动态添加他们没有的东西来满足我们的需求.(这种方法可能还有其他问题,但是当我们到达那里时我们将解决这些问题.)

摘要

  1. 我们需要检测页面上是否加载了角度,以及版本是什么.
  2. 使用systemjs,window.angular返回undefined.
  3. 角应用程序运行良好,没有问题.
  4. 使用http2,我们不认为不需要使用systemjs加载角度,但我们并不反对在必要时更改它.

javascript systemjs angular

5
推荐指数
1
解决办法
965
查看次数

标签 统计

angular ×2

javascript ×1

systemjs ×1