【发布时间】:2016-11-02 08:39:52
【问题描述】:
我正在关注component-relative paths angular documentation。
根据说明,我将组件 ts 和组件 html 保存在同一目录中。对于这个问题,该目录是 /app/components/first
我的组件ts包含以下代码,命名为First.component.ts
import {Component, Injectable} from '@angular/core';
import {MyService} from "../../service/MyService";
import {ValueObj} from "../../value/ValueObj";
@Component({
moduleId: module.id,
selector: 'relative-path',
templateUrl: 'First.component.html',
providers: [MyService]
})
@Injectable()
export class FirstComponent {
public valueObjs: ValueObj[];
constructor(private _myService: MyService) {}
getAllItems(): void {
this._myService.getAllValueObjs().subscribe(
data => this.valueObjs= data, error=> console.log(error),
() => console.log("getting complete"));
}
}
我的 First.component.html 包含以下代码:
<form (ngSubmit)="getAllItems()">
<label>Value Objects</label>
<button type="submit">Search</button>
</form>
<hr>
<p style="color:red">{{error}}</p>
<h1>All Objects</h1>
<div *ngFor="let valueObj of valueObjs">{{valueObj.name}}</div>
我看过这个解决方案,The selector "my-app" did not match any elements,但是,我发现它不适用。我的 main.ts 文件只包含这个:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
// start the application
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
并且没有对 BrowserModule 的引用。我还查看了 why getting the error "The selector did not match any elements"? ,但是,我没有 boot.ts 文件来比较我的值。
最后,我查看了 Angular2 CLI build EXCEPTION : The selector "app-root" did not match any elements ,但是,我的 Packaging.json 中没有提到 angular-universal。
知道我做错了什么吗?
【问题讨论】:
标签: javascript angular