So I have two components in Angular2. When a user clicks a button in component1, I have a method that stores a data in the sharedservice
to a variable. This variable is accessed in component2 ngOnInit()
. However the variable in initialised to undefined in ngOnInit()
because it doesn't wait for the click event.
All In all, how can I have angular2 component2 wait for click event in component1?
In JavaScript, we can easily do this using click event listener or have a callback function, but I don't know how to implement the same idea in angular.
Could you please share your ideas.
This is what I have so far:
modules.component.html
<tr *ngFor="let module of modules" #moduleObject>
<a class="test" (click)="module._clicked = !module._clicked"></a>
<ul class="dropdown-menu" role="menu" [ngStyle]="{'display': module._clicked ? 'block' : 'none'}" >
<li><a (click)="_showDialogue =! _showDialogue; _getModuleCode(module)"><img src="../../assets/img/pencil.svg" alt="" width="13px">Edit Module</a></li>
</ul>
</tr>
I'll remove unecessary code from modules.component.ts as it will just blow everything in here modules.component.ts
import {SharedService} from "../shared/shared.service";
import {DepartmentsService} from "./departments.service";
import {ActivatedRoute, Params} from "@angular/router";
import {Module} from "../dashboard/Module";
@Component({
selector: 'app-modules',
templateUrl: './modules.component.html',
providers: [DepartmentsService]
})
export class ModulesComponent implements OnInit {
service;
modules: Module[];
constructor(
private activatedRoute: ActivatedRoute,
service : SharedService,
private departmentsService: DepartmentsService,
route: ActivatedRoute) {
route.params.subscribe(p => {this.department = p['dname']; });
this.service = service;
}
_getModuleCode(moduleObject) {
this.departmentsService.moduleObject = moduleObject;
}
ngOnInit() { }
}
departments.service.ts
// assume necessary imports above
@Injectable()
export class DepartmentsService {
public _facultyName :string;
public _departmentName :string;
public moduleObject:Module;
constructor() {}
}
Then I call the moduleObject variable from this component: edit-forms.component.html
import {Module} from "./Module";
import {DepartmentsService} from "../components/departments.service";
//Change
@Component({
selector: 'enable-module-form',
templateUrl: './edit-forms.component.html',
providers:[ModuleService]
})
export class EnableModFormComponent {
constructor(
private moduleService: ModuleService,
private departmentService: DepartmentsService
) { }
ngOnInit(){
console.log(this.departmentService.moduleObject);
}
}
This should work!
DepartmentService:
private moduleSource = new Subject<any>();
moduleValue = this.moduleSource.asObservable();
moduleValueReceived(module) {
//emit value
this.moduleSource.next(module)
}
and in your parent, when you have the value set the value in the service:
_getModuleCode(moduleObject) {
this.departmentsService.moduleValueReceived(moduleObject);
}
and in your child constructor, subscribe to the value:
constructor(
private moduleService: ModuleService,
private departmentService: DepartmentsService
) {
moduleService.moduleValue.subscribe(moduleObject => {
console.log(moduleObject); // here you have your value
})
}
Here in the official docs you have the equivalent example with further explanations than I have provided.
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.