来自外部数据的Angular 2 Bootstrap应用程序 [英] Angular 2 Bootstrap application from external data

查看:104
本文介绍了来自外部数据的Angular 2 Bootstrap应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在获取外部数据后才加载Angular 2应用程序?

How to load Angular 2 application only after getting external data?

例如,在同一个html页面上有外部应用程序,我需要将一些数据传递给我应用服务。想象一下,这是 API URL ,比如'some_host / api /',在获取此信息之前,我的应用程序不应初始化。

For example, there is external application on the same html page, I need pass some data to my app service. Imagine, this is API URL, like 'some_host/api/' and my application should not be initialized till getting this information.

是否可以从外部应用程序脚本调用我的应用程序的某些方法,如:

Is it possible to call some method of the my application from external app script like:

application.initApplication('some data string', some_object);

index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>App</title>
  <base href="/">
  <link>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<script>
  application.initApplication('api/url', some_object);
</script>


  <app-root
   >Loading...</app-root>

</body>
</html>

推荐答案

以下是一些事情:
plnkr: https://plnkr.co/edit/b0XlctB98TLECBVm4wps

您可以在窗口上设置URL对象:请参阅下面的 index.html
在根组件中,添加 * ngif =ready其中ready是根组件的公共成员,默认情况下设置为false。

You can set the URL on the window object: see index.html below. In your root component, add *ngif="ready" where ready is a public member of your root component that is set to false by default.

然后使用http服务在服务/根组件中使用该URL,一旦请求成功,您可以将ready设置为true,并且您的应用将显示:请参阅 app.ts app component ngOnInit method。

Then use that URL in your service/root component with http service, once the request is successful you can set ready to true and your app will show: see app.ts app component ngOnInit method.

代码:

文件: src / app.ts

import { Component, NgModule, VERSION, OnInit } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule, Http } from '@angular/http';

@Component({
  selector: 'my-app',
  template: `
    <div *ngIf="ready">
      <h2>Hello {{name}}</h2>
    </div>
  `,
});

export class App implements OnInit {
  name: string;
  ready: boolean;
  constructor(private http: Http) {
    this.name = `Angular! v${VERSION.full}`
  }
  ngOnInit(){
    const self = this;
    const url = window["myUrl"];
    this.http.get(url)
    .subscribe(
      (res) =>
      {
        // do something with res
        console.log(res.json())
        self.ready = true;
      },
      (err) => console.error(err)),
      () => console.log("complete"))
  }
}

@NgModule({
  imports: [ BrowserModule, HttpModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

文件: src / data.json

{
  "key1": "val1",
  "key2": "val2"
}

文件: src / index.html

<header>
    ...
    <script>window['myUrl'] = 'data.json'</script>
    ...
</header>

这篇关于来自外部数据的Angular 2 Bootstrap应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆