【问题标题】:Property 'parentElement' does not exist on type 'EventTarget'“EventTarget”类型上不存在属性“parentElement”
【发布时间】:2019-08-02 12:09:25
【问题描述】:

发展促成了这项工作!!

生产模式这个错误!!

如果你删除 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); }

【问题讨论】:

    标签: typescript angular5


    【解决方案1】:

    ng-reflect-model 属性仅在开发模式下呈现,并且仅用于调试目的(请参阅What does the "ng-reflect-*" attribute do in Angular2/4?

    因此,您不应尝试在应用程序代码中访问它们。

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

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

    【讨论】:

    • 这真是天才!
    猜你喜欢
    • 2019-01-28
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2018-04-30
    • 2021-06-03
    • 2018-01-09
    相关资源
    最近更新 更多