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

时间:2017-02-21 00:55:38

标签: angular typescript angular2-forms angular2-services

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

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

在JavaScript中,我们可以使用click事件监听器轻松完成此操作或具有回调函数,但我不知道如何以角度实现相同的想法。

请您分享您的想法。

这是我到目前为止所做的:

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);
  }

}

1 个答案:

答案 0 :(得分:2)

这应该有效!

DepartmentService:

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你有与我提供的进一步解释相同的例子。

希望这有帮助!