首页 > 解决方案 > “EventTarget”类型上不存在属性“parentElement”

问题描述

开发完成这项工作!!

生产模式这个错误!!

如果删除 FocusEvent,同样的错误。

Cannot read property 'value' of undefined
at e.preSaveMac (main.bundle.js:1)
at Object.handleEvent (main.bundle.js:1)
at Object.handleEvent (main.bundle.js:1)
at Object.handleEvent (main.bundle.js:1)
at Ji (main.bundle.js:1)
at main.bundle.js:1
at t.r [as _next] (main.bundle.js:1)
at t.__tryOrUnsub (main.bundle.js:1)
at t.next (main.bundle.js:1)
at t._next (main.bundle.js:1)

preSaveMac(event:FocusEvent){ this.additionSearchMAC = event.srcElement.parentElement.attributes["ng-reflect-model"].value; $(event.srcElement).val(this.additionSearchMAC); }

标签: typescriptangular5

解决方案


ng-reflect-model属性仅在开发模式下呈现,并且仅用于调试目的(请参阅“ng-reflect-*”属性在 Angular2/4 中做了什么?

所以你不应该尝试在你的应用程序代码中访问它们。

看来您正在尝试访问 1)当前组件或 2)某个父组件的 ngModel。如果 1),只需直接使用您在模板中引用的变量(例如,([ngModel])=myVar意味着您可以this.myVar在组件代码中使用它来访问它)如果 2) 将ngModel向下传递给您使用 this 的组件@Input()

顺便说一句:使用 Angular 中的 jquery 函数为表单控件设置值val()很可能是不好的做法(如果可能,请改用 ngModel 或 FormControl)。


推荐阅读