angular - 在指令(材料)中注入宿主组件
问题描述
我可以像这样在指令构造函数中注入一个组件:
constructor(private hello: HelloComponent) {
console.log(hello.test)
}
这项工作很好,但我的指令需要专门与 Mat-Select 一起工作,所以我正在尝试这样的事情:
constructor(private matSelect: MatSelect) {
console.log(matSelect)
}
这种方式行不通:
Error: StaticInjectorError(AppModule)[SelectSearchDirective -> MatSelect]:
StaticInjectorError(Platform: core)[SelectSearchDirective -> MatSelect]:
NullInjectorError: No provider for MatSelect!
有没有办法访问 MatSelect (我需要玩 [(value)])
提前致谢。
Stackblitz:https ://stackblitz.com/edit/material-tooltip-select-search?file=src%2Fapp%2Fapp.component.html
解决方案
要访问垫子上的所有属性,请选择您需要
import {MatSelect} from '@angular/material';
在组件中执行的操作,
@ViewChild('myTemplateReference') select: MatSelect;
在 html 上你只需要添加模板引用
<mat-select #myTemplateReference [(ngModel)]="value">
有了这一切,该select
属性现在为您提供了 mat select 的所有方法和属性。
但如果你只需要价值,为什么不直接使用[(ngModel)]="value"
然后在选择更改使用(selectionChange)="doSomething()"
doSomething(){
this.value...
}
推荐阅读
- java - 在java中将svg转换为某种图像格式
- javascript - 如何在 Axios 中传递参数?
- python - 在 AppNexus API 中使用
- go - 如果我不需要错误处理,如何使用多值返回函数的结果作为 Golang 中另一个函数的参数?
- c++ - 类模板的 constexpr 运算符重载
- osgi - 从 Spring DM 1.2 Spring 3 / KARAF 4.0.10 迁移到 Blueprint、Spring 5 和 KARAF 4.2.0
- r - 如何在R中的数据框中组合两列?
- mariadb - 为什么这个事务会产生死锁?
- c++ - mmap 的简明指针算法
- ruby-on-rails - Rails date_select 未定义的方法字符串