Javascript 角度8-复制另一个零部件中的选择值
我正在学习角度,我需要将选择值传递给另一个组件中的“p”。因此,当select的值更改时,“p”元素也会更改 app.component.htmlJavascript 角度8-复制另一个零部件中的选择值,javascript,html,angular,components,Javascript,Html,Angular,Components,我正在学习角度,我需要将选择值传递给另一个组件中的“p”。因此,当select的值更改时,“p”元素也会更改 app.component.html 我认为最好的方法是使用服务绑定选定的值和段落内容。 尝试使用以下内容添加todo.service.ts: private selected: string; public get selectedValue() { return this.selected; } public setSelectedValue(value: string
我认为最好的方法是使用服务绑定选定的值和段落内容。 尝试使用以下内容添加todo.service.ts:
private selected: string;
public get selectedValue() {
return this.selected;
}
public setSelectedValue(value: string){
this.selected = value;
}
然后,在todo中更新服务中的值,
在测试中,只需将其放入:
<p>{{todoService.SelectedValue}}</p>
{{todoService.SelectedValue}
test.component.ts(子组件)
现在在html文件中使用该输入
test.component.html
<p>{{selectedOption}}</p>
您需要提供有关
todo
和test
组件之间关系的更多信息。是亲子、兄弟姐妹还是其他?他们是兄弟这能回答你的问题吗?todo可以使用select操作将服务更新为DOSERVICE.setSelectedValue(selectedValue);
<p>{{selectedOption}}</p>
<div class="contenedorTareas">
<div class="card text-white bg-primary mb-3" id="cards" style="max-width: 18rem;">
<div class="card-header">Header</div>
<div class="card-body">
<h5 class="card-title">Primary card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's
content.</p>
<select [(ngModel)]="optionSelected" name="selector" id="selector">
<option>Option 1</option>
<option>Option 2</option>
</select>
</div>
</div>
<div class="cards">
<div class="card text-white bg-primary mb-3" style="max-width: 18rem;">
<div class="card-header">Header</div>
<div class="card-body">
</div>
</div>
</div>
</div>
<app-test [selectedOption]="optionSelected"></app-test>
export class TodoComponent {
optionSelected: string;
}