【发布时间】:2016-05-26 05:10:55
【问题描述】:
全部,
这就是我所拥有的:
用户列表.component.ts
export class UserListComponent {
public userSerivce: UserService;
public deleteUser(id) {
this.userSerivce.delete(id);
}
}
用户列表.component.html
<div>
<some-reusable[deleteFunc]="delete" > </some-reusable><!-- this will bind "delete" on the user- list.component, to "deleteFunc" on SomeReusableComponent-->
</div>
some-reusable.component.ts
export class SomeReusableComponent {
@Input() deleteFunc: Function;
}
some-reusable.component.html
<button type="button" (click)="deleteFunc(entityId)" aria-label="Delete" >
<span class="glyphicon glyphicon-trash" aria-hidden="true"> </span>
</button>
这是我的问题:
当我单击按钮时,它调用 UserListComponent 上的函数就好了,但是当提到 this 时,它也引用 SomeReusableComponent,我理解为什么会发生这种情况,但我正在寻找一个解决方案?您可以在上面的示例中看到对this.userSerivce.delete(id); 的调用将失败,因为userService 在SomeReusableComponent 上不存在。
我希望上面的例子有意义。
谢谢
史蒂夫
【问题讨论】:
标签: typescript angular angular2-directives angular2-services