如何解决如何扩展指令模板
我有一个带有模板的指令,该指令可以在90%的场景中工作。但是,在某些情况下,我需要将其他字段添加到模板中。有没有一种方法可以扩展或修改现有的html模板,从而可以在不重建现有模板的90%的情况下添加其他字段
解决方法
在模板中放置ng-content
放置在components标签内的内容将显示在您具有ng-content的位置
void uploadJoystickPosition(JoystickPosition position){
Timer.periodic(new Duration(seconds: 1),(Timer t) {
DataModel dataModel = DataModel(1,getTimeInSeconds());
dataModel.joystickPosition = position;
debugPrint("Distance: ${position.distance}");
uploadData(dataModel).then(uploadResponse,onError: uploadError);
});
}
或者您可以传递模板
在您的组件中,您可以使用模板作为输入
<your-component>
This content will appear where the ng-content tag is in your component's template
</your-component>
和模板中
@Input()
myTemplate: TemplateRef<any>;
您可以通过传递它
<ng-container *ngTemplateOutlet="myTemplate" *ngIf="myTemplate"></ng-container>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。