【问题标题】:Angular2: Index.html doesn't display the file, only says "loading..."Angular2:Index.html 不显示文件,只显示“正在加载...”
【发布时间】:2016-05-07 19:46:56
【问题描述】:

我不确定如何将文件绑定到“my-app”选择器。 这是plnkr链接: http://plnkr.co/edit/2L5jSRK3adPZWTUwWdcP?p=preview

编辑:我认为如果我将模板代码放在 index.html 文件内的标签中,plunker 可以显示一些东西

我想知道是否有人知道如何将模板网址附加到 index.html 文件中,以便显示模板网址而不是“正在加载...”

index.html 文件:

<!DOCTYPE html>
<html>
  <head>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <title>Angular 2 Tour of Heroes</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="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.min.js"></script>

    <script src="https://npmcdn.com/zone.js@0.6.12?main=browser"></script>
    <script src="https://npmcdn.com/reflect-metadata@0.1.3"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.27/system.js"></script>
    <script src="https://npmcdn.com/typescript@1.8.10/lib/typescript.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>


<!-- 
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->

.ts 文件

import {Component, OnInit} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {Hero} from './hero';
import {HeroService} from './hero.service';
import {Observable} from'rxjs/Rx'



@Component({
  selector: 'my-app',
  templateUrl: 'app/biddingPage.component.html',
  styleUrls: ['app/BiddingPage.component.css'],
  providers: [HeroService],
  directives: [ROUTER_DIRECTIVES]
})



export class BiddingPageComponent{
   numberofbids = 0;
    slot1 = 0;


myFunction(slotvalue) 
 {
        this.slot1 = slotvalue;
        this.numberofbids +=1;

 }
  ngOnInit(ticks){
    let timer = Observable.timer(2000,1000);
    timer.subscribe(t=>this.ticks = t);
  }

  upgradeTime = 172801;
  ticks = this.upgradeTime;
timer(days, hoursLeft, minutesLeft, remainingSeconds) {
    days        = Math.floor(this.ticks/24/60/60);
    hoursLeft   = Math.floor((this.ticks) - (days*86400));
    var hours       = Math.floor(hoursLeft/3600);
    minutesLeft = Math.floor((hoursLeft) - (hours*3600));
    var minutes     = Math.floor(minutesLeft/60);
    remainingSeconds = this.ticks % 60;
    if (remainingSeconds < 10) {
        remainingSeconds = 0 + remainingSeconds; 
    }
    document.getElementById('countdown').innerHTML = days + ":" + hours + ":" + minutes + ":" + remainingSeconds;
    if (this.ticks == 0) {
        clearInterval(this.countdownTimer);
        document.getElementById('countdown').innerHTML = "Completed";
    } else {
        this.ticks--;
    }
}
 countdownTimer = setInterval('timer()', 1000);

}

【问题讨论】:

    标签: javascript html angular


    【解决方案1】:

    你的配置文件有点乱。

    将您的文件目录从src 更改为app。例如src/main.tsapp/main.ts

    将您的 index.html 脚本标签更改为此

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

    和你的 config js 到这里

    (function(global) {
    
    var  map = {
        'app':                        'app', // 'dist',
        'rxjs':                       'https://npmcdn.com/rxjs@5.0.0-beta.6',
        'angular2-in-memory-web-api': 'https://npmcdn.com/angular2-in-memory-web-api' // get latest
      };
    
    var packages = {
        'app':                        { main: 'main.ts',  defaultExtension: 'ts' },
        'rxjs':                       { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { defaultExtension: 'js' },
      };
    
    var packageNames = [
          '@angular/common',
          '@angular/compiler',
          '@angular/core',
          '@angular/http',
          '@angular/platform-browser',
          '@angular/platform-browser-dynamic',
          '@angular/router',
          '@angular/router-deprecated',
          '@angular/upgrade',
      ];
    
    
      packageNames.forEach(function(pkgName) {
        map[pkgName] = 'https://npmcdn.com/' + pkgName + ngVer;
      });
    
    
      packageNames.forEach(function(pkgName) {
        packages[pkgName] = { main: 'index.js', defaultExtension: 'js' };
      });
    
       var config = {
        transpiler: 'typescript',
        typescriptOptions: {
          emitDecoratorMetadata: true
        },
        map: map,
        packages: packages
      }
    
    
      if (global.filterSystemConfig) { global.filterSystemConfig(config); }
    
      System.config(config);
    
    })(this);
    

    【讨论】:

    • 我仍然收到“正在加载...” 我要删除 index.html 文件中的所有其他脚本还是保留它们?
    • 您是否也更改了app.ts 的目录?从src/app.tsapp/app.ts
    • 目录位于配置文件的哪一行?
    • 如何更改目录
    【解决方案2】:

    你的 plunker 和代码有一些错误,这里是一个干净的plunker with your code

    这是如何在angular 2 Component中指定templateUrl和StyleUrl

    @Component({
    
     selector: 'my-app',
     templateUrl:"src/app.component.html",
     styleUrls: ['src/app.component.css'],
    
    
     })
    

    要更改 plunker 中的目录或文件名,只需双击它即可。

    【讨论】:

      猜你喜欢
      • 2016-09-13
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多