javascript - 如何在 Angular 6 中从父级的 HTML 编辑共享组件

标签 javascript angular

child.component.ts

//ignoring all imports

@component({
   selector: 'app-child',
   template: './child.component.html'
})

import class ChildComponent {

   //logic not of use

   public isData: boolean = false;
}

child.component.html

<input type="text" value="xyz" [readonly]="isData">

parent.component.ts

//ignoring all imports

parent.component.html

<app-child></app-child>

在这里,我想在父级内部使用子组件,但我不想要输入字段只读,而是在子级内部更改它我想在父级内部更改它,以便我的子组件应该不被打扰。

我将如何实现它?

最佳答案

您应该在子组件中提供一些参数来打开/关闭只读属性 - 例如

child.component.html

<input type="text" value="xyz" [readonly]="isData || onlyForRead">

子组件.ts

@Input('onlyForRead') onlyForRead: boolean;

parent.component.html

<app-child [onlyForRead]='false'></app-child>

关于javascript - 如何在 Angular 6 中从父级的 HTML 编辑共享组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57596270/

相关文章:

javascript - JQuery 无法处理按钮的点击事件

javascript - 循环遍历元素集并从不同数组添加适当的值

javascript - svg 路径 css 动画暂停,jquery 运行

angularjs - Angular 2 : How to link directly from the root view to a (grand)child view

angular - 行为主题订阅加一

angular - 开源 Angular 树组件

javascript - 单例在 JS/coffeescript 中引用自身的优雅方式?

javascript - Ember-cli 如何添加 FileSaverjs 和 Blobjs

html - 将 HTML 内容保存到数据库是个好主意吗?

javascript - 无法从 TS 文件访问外部 JS 文件的功能