简体   繁体   English

为什么在调用有效函数时出现类型错误? | 角度2

[英]Why do I get a Type Error when calling a valid function? | Angular 2

Every time I call this function I receive an error telling me that "TypeError: co.newMethod2 is not a function" 每次调用此函数时,我都会收到一条错误消息,告诉我“ TypeError:co.newMethod2不是函数”

I don't understand why as my method is valid in my type script file. 我不明白为什么在我的类型脚本文件中我的方法有效。 Here is my code 这是我的代码

create-sharecase.component.html create-sharecase.component.html

<div> 
    <p> Template Description </p>
     <textarea [ngModel]="textValue" (ngModelChange)="newMethod2()">
    </textarea>
</div>

create-sharecase.component.ts create-sharecase.component.ts

newMethod2(){
    console.log("Text Area Method");
if (this.textValue != '') {
    console.log("Text Value" + this.textValue);
}}`

To me this should be valid code and should execute! 对我来说,这应该是有效的代码,应该执行! All hep will be appreciated! 所有麻将不胜感激!

It seems the way you wrote your method is wrong, here's what I propose : 看来您编写方法的方式是错误的,这是我的建议:

this.newMethod2 = () => {
    console.log("Test Area  Method");
    if (this.textValue != ''){
        console.log("Other text");
    }
};

USe ngchange event for call a function when change any artibutes or change angular function ... try this 在更改任何工件或更改角度函数时使用ngchange事件来调用函数...请尝试此操作

**strong text**
<div> 
    <p> Template Description </p>
     <textarea ngModel="textValue" ngChange="newMethod2()"></textarea>
</div>

and angular .. 和角..

function newMethod2 ()  {
    console.log("Test Area  Method");
    if (this.textValue != ''){
        console.log("Other text");
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应为什么我在调用作为参数传递的 function 时收到错误“Uncaught TypeError: createTask is not a function”? - REACT Why do I get the error "Uncaught TypeError: createTask is not a function" when calling a function passed as a parameter? 为什么在调用不带参数的函数时出现未定义的错误? - Why do I get an undefined error when calling a function without parameter? 为什么在创建 function 时会出现错误,但在创建变量时却不会? - Why do I get an error when create a function but not when a variable? 为什么在bloxk范围之外调用函数时没有收到ReferenceError? - Why I do not get ReferenceError when calling a function outside a bloxk scope? 当我在获得输入类型的函数中输入{}时,为什么我会得到未定义的 - why do i get undefined when i input {} in side a function that get the type of input 为什么我会通过索引获取语法错误调用对象项? - Why do I get syntax error calling object item by index? 为什么会出现错误:“ .isValid()”不是一个函数? - Why do I get the error: “.isValid()” is not a function? 为什么我会收到“... is not a function at HTMLButtonElement”错误? - Why do I get "... is not a function at HTMLButtonElement" error? 为什么会出现“不是函数”错误? - Why do I get “is not a function” error? 为什么我在返回函数中收到此错误? - Why do I get this error in the return function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM