angular.js - Angular 2 appInjector menyebabkan isu ralat
漂亮男人
漂亮男人 2017-05-15 16:53:52
0
2
688

Mengikut https://angular.io/docs/js/latest/guide/displaying-data.html langkah demi langkah, terdapat masalah dengan kelas rujukan.

Kompilasi TypeScript:

tsc --watch -m commonjs

"compilerOptions": {
    "emitDecoratorMetadata": true,
    "module": "amd",
    "target": "es5"
}

Kod asal ialah:

/// <reference path="typings/angular2/angular2.d.ts" />
import {Component, View, bootstrap, NgFor} from 'angular2/angular2';

@Component({
  selector: 'display',
  appInjector: [FriendsService]
})

@View({
  template: `
  <p>My name is {{ myName }}</p>
  <p>Friends:</p>
  <ul>
     <li *ng-for="#name1 of names">
        {{ name1 }}
     </li>
  </ul>
  `,
  directives: [NgFor]
})

class DisplayComponent {
  myName: string;
  names: Array<string>;
  constructor(friendsService: FriendsService) {
    this.myName = 'Alice';
    this.names = friendsService.names;
  }
}

class FriendsService {
  names: Array<string>;
  constructor() {
    this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"];
  }
}

bootstrap(DisplayComponent);
漂亮男人
漂亮男人

membalas semua(2)
黄舟

Alihkan sahaja FriendsService ke atas sana

仅有的幸福

https://github.com/kittencup/angular2-ama-cn/issues/11 mempunyai penyelesaian khusus

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan