angular - 如何在 Angular 中以编程方式检查组件是否具有属性定义?
问题描述
我在 Angular 中有一个组件的属性,如下所示:
export class CarComponent implements OnInit {
partNumber: number;
checkIfItHasAPartNumberProperty() {
return this.hasAProperty('partNumber');
}
hasAProperty 检查此类是否包含按字符串名称定义的属性的 Angular 等效项是什么,例如。“零件号”?
这不是关于获取“partNumber”值的问题,也不是直接检查它是否具有属性“partNumber”的问题,而是以编程方式检查组件是否具有给定名称的属性(通过字符串)。
解决方案
const obj = this as object;
obj.hasOwnProperty('prop')
Cast this to object and you should be able to call hasOwnProperty on it
EDIT This wont work on properties that haven't been initialized. For that case (if possible in your scenario) initialize the property with null and the above code will work
推荐阅读
- android - 如何在应用程序更新期间进行领域数据库迁移而不使用 xamarin 删除数据?
- tensorflow - 使用 tensorflow 构建语言模型,数据集形状问题
- javascript - 如何在我的网络应用程序中集成 github 登录(JavaScript)
- iphone - 使用 browserstack 在 Iphone/iPad 真实设备上执行量角器测试用例时出错
- c# - 当 ObservableCollection 中的属性发生变化时引发事件
- json - friend.toLowerCase 不是函数。应用离子的搜索功能时出错
- php - 具有特定值句柄的时间选择字段
- mysql - Docker tomcat镜像无法访问远程mysql服务器
- python - 在 Tensorflow RNN 中,logits 和标签必须是可广播的错误
- react-native - 隐藏 createBottomTabNavigator 上的选项卡(navigationOptions:{tabBarVisible:false})不起作用