angular - 如何在angular2中的同一组件中定义自定义属性

标签 angular

我正在尝试定义自定义属性count。但以下给出错误:无法绑定(bind)到“count”,因为它不是“p”的已知属性。如何消除此错误并使 count 成为 <p> 的自定义属性

other.component.html

<p [count] = "10">
  other works!
</p>

other.component.ts

import {Component, Input, OnInit} from '@angular/core';

@Component({
  selector: 'app-other',
  templateUrl: './other.component.html',
  styleUrls: ['./other.component.css']
})
export class OtherComponent implements OnInit {

  @Input() count = 10;

  constructor() { }

  ngOnInit() {
  }

}

最佳答案

<p>没有 name属性(property)。 您可以绑定(bind)到 name属性使用

[attr.name]="name"

或者替代地

attr.name="{{name}}"

仅将此第二种形式(插值)用于绑定(bind)字符串值,因为传递的值将始终被字符串化。

关于angular - 如何在angular2中的同一组件中定义自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43898344/

相关文章:

angular - 如何捕捉 Observable.forkJoin(...) 中的错误?

Angular ng-template 用作语法

javascript - 通过 View 中的 @ViewChild 调用组件上的方法到 QueryList

javascript - 如何从动态加载的模式向父级发出事件

angular - 使用基本 href 从不同的子目录提供 angular2

javascript - 如何防止带有 Moment Js 的 Angular Material 6 DatePicker 转换不正确的格式?

html - 如何在 ionic 中获取输入文本值

Angular2 RC6 禁用输入

angular - *ngFor 使用一个函数,返回一个循环

css - Angular Material Sortable Table——由于一些我无法解决的全局问题,排序箭头没有显示,我错过了什么?