angular - 如何在angular2中等待用户点击事件?

标签 angular typescript angular2-forms angular2-services

所以我在 Angular2 中有两个组件。当用户单击 component1 中的按钮时,我有一个方法将 sharedservice 中的数据存储到变量中。在 component2 ngOnInit() 中访问此变量。但是,变量在 ngOnInit() 中初始化为未定义,因为它不等待点击事件。

总而言之,我如何让angular2 component2等待component1中的点击事件?

在 JavaScript 中,我们可以使用点击事件监听器或回调函数轻松地做到这一点,但我不知道如何在 Angular 中实现相同的想法。

能否请您分享您的想法。

这是我目前所拥有的:

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>

我将从 modules.component.ts 中删除不必要的代码,因为它会破坏这里的所有内容 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() {}
}

然后我从该组件调用 moduleObject 变量: 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);
  }

}

最佳答案

这应该有效!

部门服务:

private moduleSource = new Subject<any>();
moduleValue = this.moduleSource.asObservable();

moduleValueReceived(module) {
  //emit value
  this.moduleSource.next(module)
}

在你的父级中,当你有值时在服务中设置值:

_getModuleCode(moduleObject) {
   this.departmentsService.moduleValueReceived(moduleObject);
}

并在您的子构造函数中订阅值:

constructor( 
  private moduleService: ModuleService, 
  private departmentService: DepartmentsService 
) { 
     moduleService.moduleValue.subscribe(moduleObject => {
        console.log(moduleObject); // here you have your value
     })
  }

Here in the official docs你有比我提供的更多解释的等效示例。

希望这对您有所帮助!

关于angular - 如何在angular2中等待用户点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42356829/

相关文章:

css - Angular2 CSS :host applying but not visible in browser

javascript - Angular 5 页面刷新总是登陆首页而不是在同一页面

dart - 是否可以设置控件的有效属性?

Angular 树摇晃 : How exactly does it work?

angular - 比较Angular 6中的两个日期

class - 在 Typescript 中声明类类型变量

typescript - Mongoose - 无法访问 createdAt

javascript - 属性 Angular 2 的绑定(bind)不会更新具有绑定(bind) ID 的关联输入

angular - 添加/删除响应式(Reactive)表单验证器以动态创建输入

angular - FormControl 在 Angular 2 中返回空值