Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript";匹配“;角度误差2_Javascript_Angular_Typescript - Fatal编程技术网

Javascript";匹配“;角度误差2

Javascript";匹配“;角度误差2,javascript,angular,typescript,Javascript,Angular,Typescript,我收到typeerror:无法读取未定义的“match”属性 var numInput = document.getElementById('input'); // Listen for input event on numInput. numInput.addEventListener('input', function(){ // Let's match only digits. var num = this.value.match(/^\d+$/); if (nu

我收到typeerror:无法读取未定义的“match”属性

var numInput = document.getElementById('input');

// Listen for input event on numInput.
numInput.addEventListener('input', function(){
    // Let's match only digits.
    var num = this.value.match(/^\d+$/);
    if (num === null) {
        // If we have no match, value will be empty.
        this.value = "";
    }
}, false)

此.value未定义,因为您没有使用箭头函数(=>)。试试这个

numInput.addEventListener('input', () => {
// Let's match only digits.
var num = this.value.match(/^\d+$/);
if (num === null) {
    // If we have no match, value will be empty.
    this.value = "";
}
}, false)

这很可能是一个范围问题。当您对函数使用大括号表示法时,将创建一个新的作用域。因此,
此.value
未定义。使用箭头函数表示法可以避免这种情况

numInput.addEventListener('input', () => {
    // Let's match only digits.
    var num = this.value.match(/^\d+$/);
    if (num === null) {
        // If we have no match, value will be empty.
        this.value = "";
    }
}, false)

@yurzui-Nooo!那一定会打破它的!。当我测试它时,它工作得很好。你需要提供一个真实的答案。你的不完整。你确定angular是这个问题的正确标记吗?@Quentin我没有注意到属性名称:)我以为他使用了一些组件属性而不是
输入。值
不,我有错误。当我在angular 2中执行时,没有迹象表明此
值被定义为外部范围内的任何
值。另一方面,事件侦听器的
this
值将是触发事件的元素,这是一个输入元素,它将具有一个
value
属性,该属性有效:没有任何迹象表明
this.value
是为外部范围中的任何
定义的。另一方面,事件侦听器的
this
值将是触发事件的元素,这是一个输入元素,它将具有
value
属性,可以工作:当然,在html和javascript中工作良好。当我将javascript与angular 2 typescript一起使用时,它不工作。