Angular LifecycleHook:OnChanges-不带@input的触发器

Angular LifecycleHook:OnChanges-不带@input的触发器,angular,typescript,Angular,Typescript,我已经试过了生命周期钩子,看到了吗 当角度(重新)设置数据绑定输入属性时响应 我现在的问题是:是否有可能在非输入属性上触发它,从而改变它们在html中的值? 为什么它不能像下面那样工作 我有这些文件: Html OnChangesTestComponent: export class HtmlComponent implements { power: string; @ViewChild(OnChangesComponent) childView: OnChangesComponent;

我已经试过了生命周期钩子,看到了吗

当角度(重新)设置数据绑定输入属性时响应

我现在的问题是:是否有可能在非输入属性上触发它,从而改变它们在html中的值? 为什么它不能像下面那样工作

我有这些文件:

Html

OnChangesTestComponent:

export class HtmlComponent implements {
  power: string;
  @ViewChild(OnChangesComponent) childView: OnChangesComponent;
}
export class OnChangesTestComponent implements OnChanges {
  @Input() power: string;

  ngOnChanges(changes: SimpleChanges) {
    ...
  }
}
我想总结一下:

Html


只要使用单向数据绑定,即可使视图在值更改时更新:

 //html 
  <p>{{power}}</p> 
   //one way databinding to the power variable in your .ts file
   //the names in your .ts and html must be the same
//html
{{power}}

//单向数据绑定到.ts文件中的power变量 //.ts和html中的名称必须相同

要更改该值,应使用BehaviorSubject将“power”值从输入传递到其他组件。有关

的详细信息,您需要
doCheck
对于一般的脏检查,
onChanges
仅用于输入更改,请小心使用
doCheck
@ABOS。我建议使用它作为最后的资源<代码>虽然ngDoCheck()钩子可以检测到英雄的名字何时发生了变化,但它的成本非常高@ABOS尽力避免使用Dockeck,因为即使没有变化也会调用它。@LeandroLima,我认为性能不是问题。通常情况下,您会检查更改,这是一个简单的if(…),我不认为一般来说调用此函数代价高昂
<html>
  <input ([ngModel])="power"></input>
</html>
export class HtmlComponent implements OnChanges {
  power: string;

  ngOnChanges(changes: SimpleChanges) {
    ...
  }
}
 //html 
  <p>{{power}}</p> 
   //one way databinding to the power variable in your .ts file
   //the names in your .ts and html must be the same