javascript – AngularJs使用Input组件传递数据

我正在尝试使用
scotch.io网站上的免费视频教程学习AngularJs 2.我跟着大部分没有任何问题.但我无法将数据从一个组件传递到另一个组件.

app.component.html中列出了用户配置文件.当用户单击用户链接时,它应显示文本输入,用户可以在其中更改名称.

这是app.component.html

<header>
  <nav class="navbar navbar-inverse">
    <div class="navbar-header">
      <a href="/" class="navbar-brand">My Angular 2 App</a>
    </div>
  </nav>
<main>

  <div class="row">
    <div class="col-sm-4">
      <div *ngIf="user_list">
        <ul class="list-group users-list" >
          <li class="list-group-item" *ngFor="let user of user_list"
          (click)="selectUser(user)"
          [class.active] = "user == active_user">{{user.name}}</li>
        </ul>
      </div>
    </div>
    <div class="col-sm-8">

      <user-profile [user] = "active_user"></user-profile>

      <div class="jumbotron gocrazy" *ngIf="!active_user">
        <span class="glyphicon glyphicon-hand-left"></span>
        <h2>Choose a user from the left menu </h2>
      </div>
    </div>
  </div>


</main>

<footer class="text-center">
  Copyright &copy; 2016
</footer>

这是user-profile.component.ts文件.

import {Component, Input} from '@angular/core';
import {User} from '../shared/model/user';

@Component({
  selector : 'user-profile',
  template : `
  <div class="jumbotron gocrazy" *ngIf="user">
    <h1>Welcome to our app</h1>
    <p>{{user.name}}</p>
    <p>{{message}}</p>
    <input class="form-control" [(ngModel)] = "user.name" />
  </div>
  `
})

export class UserProfileComponent{
  @Input() user : User;
}

这是app.component.ts文件.

import {Component} from '@angular/core';
import {User} from './shared/model/user';

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

export class AppComponent {
  message : string = "Hello, How are you?";

  user_list : User[] =[
  {
    name : "Thilini Weerasooriya",
    age : 27,
    id : 2
  },
  {
    name : "Aurelia",
    age : 23,
    id : 2
  }];

  active_user : User;

  selectUser(user){
    this.active_user = user;
  }

}

这是app.module.ts文件.

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';
import {AppComponent} from './app.component';
import {UserProfileComponent} from './users/user-profile.component';

@NgModule({
  imports : [BrowserModule, FormsModule],
  declarations : [AppComponent, UserProfileComponent],
  bootstrap : [AppComponent, UserProfileComponent]
})

export class AppModule{}

文件夹结构如下.

《javascript – AngularJs使用Input组件传递数据》

而且我没有任何错误.

《javascript – AngularJs使用Input组件传递数据》

我知道我应该调试代码,但是我对Angular和TypeScript知之甚少.如果您对如何调试或解决问题有任何想法或提示,那就太棒了.

谢谢!

最佳答案 您应该只引导AppComponent.测试了你的代码,看起来,如果你引导两个组件,就不会抛出错误.

此外,修复后,您需要将模式添加到ngModule.这是因为您创建了一个自定义(html)标记,该标记未被识别为“普通”html标记.如果您不声明它,您的应用程序将抛出错误,因为它无法识别您的自定义标记.话虽这么说,你的ngModule应该是这样的:

//import CUSTOM_ELEMENTS_SCHEMA
import {NgModule, CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';
import {AppComponent} from './app.component';
import {UserProfileComponent} from './users/user-profile.component';

@NgModule({
  imports : [BrowserModule, FormsModule],
  declarations : [AppComponent, UserProfileComponent],
  bootstrap : [AppComponent]
  schemas: [CUSTOM_ELEMENTS_SCHEMA] // add this!
})

export class AppModule{}

这是一个工作plunker

点赞