【问题标题】:Issue with template in sharedVar in angular角度中 sharedVar 中的模板问题
【发布时间】:2018-10-26 01:28:55
【问题描述】:

我想知道是否只需要在模板标签中实现这个

 template: ` <p>Child: {{sharedVar}}</p> 
 <input [(ngModel)]="sharedVar" 
  (ngModelChange)="change()">

...因为在我的组件中我有

 @Component({
  selector: 'app-transaction',
  templateUrl: './transaction.component.html',
  styleUrls: ['./transaction.component.scss', ],

  })  

像这样。我可以直接在我的html中实现它吗

我是从 -

Angular 2 ngModel in child component updates parent component property

【问题讨论】:

  • 您不能在同一个组件中同时使用templateUrltemplate。您必须使用templateUrltemplate。区别在于templateUrl 的情况下,您在.ts 文件之外写入html template,而在template 的情况下,您将html template 写为.ts 内部的字符串。否则就没什么区别了
  • 是的,你需要将它插入'transaction.component.html',并且你必须从'@angular/forms'导入import {FormsModule};

标签: angular


【解决方案1】:

不,您不能在一个组件中同时使用这两个属性,使用 templateUrltemplate 因为 template 是内联编辑,templateUrl 是 html 页面内编辑。所以你只能选择一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-22
    • 2015-09-12
    • 1970-01-01
    • 2017-01-22
    • 2020-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    相关资源
    最近更新 更多