html - 当 Angular 中的输入发生变化时更新组件
问题描述
<div class=" card-body">
<div class="row">
<div class=" font-icon-list col-lg-2 col-md-3 col-sm-4 col-xs-6 col-xs-6" routerLinkActive="active"
*ngFor="let subject of subjects">
<div class=" font-icon-detail">
<div (click)="setSubject(subject.title)" >
<i class="deep-icons {{ subject.icon }}"></i>
<p>{{ subject.title }}</p>
</div>
</div>
</div>
</div>
</div>
<subject-component [setSubjectService]="selectedSubject"></subject-component>
上面的代码来自我的 selector.component.html。
export class SubjectComponent implements OnInit {
@Input() public set setSubjectService(_subjectService: ISubjectService) {
this.subjectService = _subjectService;
}
public subjectService: ISubjectService;
constructor() {}
public ngOnInit(): void {
}
}
上面的代码来自我的 subject.component.ts
现在,主题组件在应用程序启动时被渲染一次,但每当“selectedSubject”更改时,它不会更新或重新渲染。我怎样才能做到这一点?
(使用 Angular 8)
解决方案
我认为您想使用两种方式绑定。如果是这样; 使用 eventemitter 创建一个输出,例如;
@Input() myVariable: number;
@Output() myVariableChange = new EventEmitter(); //define it nameofyourvariable+Change
constructor() { }
ngOnInit() {}
changeMyVar(v: number) {
this.myVariable=v;
this.myVariableChange.emit(myVariable);
}
并使用带括号的两种方式绑定。我的意思是如下:
<subject-component [(setSubjectService)]="selectedSubject"></subject-component>
推荐阅读
- javascript - javascript中带有字符串的嵌套数组形成
- sublimetext3 - 突出显示匹配“引号”和“双引号”
- sql - 根据其他几列的条件计算不同的一列
- robotframework - 在robotframework中并行执行KW
- python - 在python中打印异常的正确方法是什么?
- go - 无法使用流发送块
- vue.js - 如何手动比较两条路线
- python - pandas 中的加权平均值 - 字符串索引必须是整数
- rest - 如何在 POST Query 中使用 REST API 获取作者字段值
- jmeter - JMeter将响应保存到具有自定义扩展名的文件