我在从父组件重置子组件中的字段时遇到了一些困惑。我在子组件中有一个字段,当调用父组件中的函数时,子字段应该重置。子组件:
@Component({
selector: 'my-app',
template: `
<input type="text" placeholder="Type your name..." [formControl]="Name"/>
`})
export class ChildComponent {
Name = new FormControl('');
}Run Code Online (Sandbox Code Playgroud)
父组件:
@Component({
selector: 'my-parent-app',
template: `
`})
export class ParentComponent {
resetName(){
// Here I need to reset my child component field..
}
}Run Code Online (Sandbox Code Playgroud)
如果有人有想法,你能帮我理解..
在这里,我有是或否问题的列表,并提供单选按钮供用户单独选择每个问题的答案 - 还提供“全部是”和“全部否”两个按钮。
单独选择很好,但我不知道如何绑定“全是”或“全否”按钮并收集每个问题的值。
<div *ngFor="let question of questionsList; let i=index; ">
<label>
{{question.id}}
<input type="radio" [name]="i + 1">
<span>Yes</span>
<input type="radio" [name]="i + 1">
<span>No</span><br>
</label>
</div>
<div style="text-align: center">
<button type="button">All Yes</button>
<button type="button">All No</button>
<button type="button">Submit</button>
<button type="button">Clear</button>
</div>
Run Code Online (Sandbox Code Playgroud) 你好,我基本上是 Angular 的新手,在这里我试图从服务访问一个布尔变量。一个值为 true 的布尔变量,我已在服务中声明。根据某些逻辑,我将把该服务变量更改为 false。我在几个组件中使用的那个特定变量。所以我面临着这样的问题,我分配为 true 的初始值会出现在我使用过的所有组件中,但是在更改未反映在所有组件中的值之后。服务类是这样的:
@Injectable()
export class MyService {
running: boolean = true;
}Run Code Online (Sandbox Code Playgroud)
组件 1:
export class FirstComponent {
constructor(private myService: MyService) { }
check(){
this.myservice.running = false;
}
}Run Code Online (Sandbox Code Playgroud)
组件 2:
export class SecondComponent implements OnChanges {
running;
constructor(private myService: MyService) { }
ngOnChanges(){
this.running = this.myService.running;
console.log('running', this.running);
}
}Run Code Online (Sandbox Code Playgroud)
这就是我将如何更改布尔值并从不同组件中的服务访问。我不确定这是否是正确的方法,请纠正我。