如何在angular 4中使用共享html?

如何在angular 4中使用共享html?,angular,Angular,我试图在基本angular4页面中使用共享html, 我想用标签 <search-items (search)="filter($event,'q')"></search-items> 其中应显示搜索选项的文本框 我应该怎么做才能删除我的错误?自己试试这段代码:这是你的app.ts //our root app component import {Component, NgModule, VERSION} from '@angular/core' impo

我试图在基本angular4页面中使用共享html, 我想用标签

  <search-items (search)="filter($event,'q')"></search-items> 

其中应显示搜索选项的文本框


我应该怎么做才能删除我的错误?

自己试试这段代码:这是你的app.ts

//our root app component
 import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {SearchComponent} from 'search.ts'
import {FormsModule } from '@angular/forms'
 @Component({
 selector: 'my-app',
 template: '
 <div>
  <h2>Hello {{name}}</h2>
  <search-items (search)="filter($event,'q')"></search-items> 
     </div>',
 declare:[SearchComponent]
})
 export class App {
 name:string;
 constructor() {
 this.name = `Angular! v${VERSION.full}`
 }
 filter(text:string,text2:string){
  console.log("hi")
}
}
 @NgModule({
imports: [ BrowserModule , FormsModule],
declarations: [ App , SearchComponent ],
bootstrap: [ App ]
})

 export class AppModule {}
//我们的根应用程序组件
从“@angular/core”导入{Component,NgModule,VERSION}
从“@angular/platform browser”导入{BrowserModule}
从'search.ts'导入{SearchComponent}
从“@angular/forms”导入{FormsModule}
@组成部分({
选择器:“我的应用程序”,
模板:'
你好{{name}
',
声明:[搜索组件]
})
导出类应用程序{
名称:字符串;
构造函数(){
this.name=`Angular!v${VERSION.full}`
}
过滤器(文本:字符串,文本2:字符串){
控制台日志(“hi”)
}
}
@NGD模块({
导入:[BrowserModule,FormsModule],
声明:[应用程序,搜索组件],
引导:[应用程序]
})
导出类AppModule{}
第二个修复是在search.ts中:您的search.html拼写错误为search.component.html


注意:在代码中使用ngModel时,请从@Angle/forms导入FormsModule:这是您的app.ts

//our root app component
 import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {SearchComponent} from 'search.ts'
import {FormsModule } from '@angular/forms'
 @Component({
 selector: 'my-app',
 template: '
 <div>
  <h2>Hello {{name}}</h2>
  <search-items (search)="filter($event,'q')"></search-items> 
     </div>',
 declare:[SearchComponent]
})
 export class App {
 name:string;
 constructor() {
 this.name = `Angular! v${VERSION.full}`
 }
 filter(text:string,text2:string){
  console.log("hi")
}
}
 @NgModule({
imports: [ BrowserModule , FormsModule],
declarations: [ App , SearchComponent ],
bootstrap: [ App ]
})

 export class AppModule {}
//我们的根应用程序组件
从“@angular/core”导入{Component,NgModule,VERSION}
从“@angular/platform browser”导入{BrowserModule}
从'search.ts'导入{SearchComponent}
从“@angular/forms”导入{FormsModule}
@组成部分({
选择器:“我的应用程序”,
模板:'
你好{{name}
',
声明:[搜索组件]
})
导出类应用程序{
名称:字符串;
构造函数(){
this.name=`Angular!v${VERSION.full}`
}
过滤器(文本:字符串,文本2:字符串){
控制台日志(“hi”)
}
}
@NGD模块({
导入:[BrowserModule,FormsModule],
声明:[应用程序,搜索组件],
引导:[应用程序]
})
导出类AppModule{}
第二个修复是在search.ts中:您的search.html拼写错误为search.component.html

注意:当您在代码中使用ngModel时,请务必从@angular/forms导入FormsModule。此外,这是解决问题的新方法

您还需要导入FormsModule

`import { FormsModule } from '@angular/forms'`
除此之外,新的问题也得到了解决

您还需要导入FormsModule

`import { FormsModule } from '@angular/forms'`

你得到了什么错误请指定它们你可以运行给定的链接,使用控制台窗口显示错误很多东西丢失了,我可以删除错误。现在我将发布答案我只是在学习你会遇到什么错误请指定它们你可以运行给定的链接,使用控制台窗口显示错误很多东西丢失了,我可以删除错误。现在我将发布答案我正在学习一些东西有一个问题,请尝试输入一些内容,然后单击按钮并检查console.log。然后输入一些内容并单击enter。再次检查控制台,在第二种情况下,你们可以看到函数触发了两次。你们确定吗?因为我完全按照你们说的做了,它仍然只触发了一次。能否请您进行更多调试并提供更多信息,以便我可以帮助您?您是否检查了console.log?让我们来看看。有一个问题,请尝试输入一些内容,然后单击按钮并检查console.log。然后输入一些内容并单击enter。再次检查控制台,在第二种情况下,你们可以看到函数触发了两次。你们确定吗?因为我完全按照你们说的做了,它仍然只触发了一次。你能不能请你调试更多,并提供更多的信息,以便我可以帮助你?你有没有检查控制台。日志?让我们。