Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.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
Angular 角度7:setTimeout方法在选择标记中不起作用_Angular - Fatal编程技术网

Angular 角度7:setTimeout方法在选择标记中不起作用

Angular 角度7:setTimeout方法在选择标记中不起作用,angular,Angular,我想在select标记中使用setTimeout()方法,当我更改任何选项时,我想在setTimeout方法中设置select选项的其他默认选定值 我也可以使用函数来实现,但我不想在组件文件中编写函数 它应该像javascript一样可行 <button onclick="setTimeout(function(){ alert("Hello"); }, 3000))">Try it</button> 试试看 Html文件 <div class="form-gr

我想在select标记中使用
setTimeout()
方法,当我更改任何选项时,我想在
setTimeout
方法中设置select选项的其他默认选定值

我也可以使用函数来实现,但我不想在
组件
文件中编写函数

它应该像javascript一样可行

<button onclick="setTimeout(function(){ alert("Hello"); }, 3000))">Try it</button>
试试看
Html文件

<div class="form-group">
<label>Select Type</label>
<select 
    [name]="contact.contactMethod" 
    [id]="contact.contactMethod" 
    class="form-control"
    [(ngModel)]="contact.contactMethod">
    (change)="setTimeout('alert(\'Hello!\')', 500)" // here alert not working, i want to assign other select option value here
    <option *ngFor="let method of contactMethodsArray" [value]="method.id">{{ method.label }}</option>
</select>

选择类型
(更改)=“setTimeout(\'Hello!\'),500)”//此处警报不起作用,我想在此处分配其他选择选项值
{{method.label}

这里有可能发出警报吗?有人能帮我解决这些问题吗?

该语句是component类,但在componet中没有名为
setTimeout
的函数,因此它不会像您预期的那样工作,请检查控制台,您可以看到一些错误

在组件中创建一个简单的函数,该函数调用
setTimeout
,并在templatw中使用它

模板:

<div class="form-group">
<label>Select Type</label>
<select 
    [name]="contact.contactMethod" 
    [id]="contact.contactMethod" 
    class="form-control"
    [(ngModel)]="contact.contactMethod">
    (change)="someFunction()" 
    <option *ngFor="let method of contactMethodsArray" [value]="method.id">{{ method.label }}</option>


或者,您也可以使用本机
onchange
(不推荐)方法

<div class="form-group">
<label>Select Type</label>
<select 
    [name]="contact.contactMethod" 
    [id]="contact.contactMethod" 
    class="form-control"
    [(ngModel)]="contact.contactMethod">
    onchange="setTimeout('alert(\'Hello!\')', 500)" 
    <option *ngFor="let method of contactMethodsArray" [value]="method.id">{{ method.label }}</option>

选择类型
onchange=“setTimeout('alert(\'Hello!\'),500)”
{{method.label}


如果要调用select then的更改事件函数,可以通过以下方式执行

demo.html

<div class="form-group">
<label>Select Type</label>
<select 
    [name]="contact.contactMethod" 
    [id]="contact.contactMethod" 
    class="form-control"
    [(ngModel)]="contact.contactMethod">
    onchange="setTimeout('alert(\'Hello!\')', 5000)"
    <option *ngFor="let method of contactMethodsArray" [value]="method.id">{{ method.label }}</option>
</select>

选择类型
onchange=“setTimeout('alert(\'Hello!\'),5000)”
{{method.label}

我希望这将是有用的。

Hi@Pranav我已经更新了我的问题,请检查我希望作为javascript@BhagwatTupe:然后您可以依赖于
onchange=“setTimeout(\'Hello!\'),500)”
Hi我也尝试过
onchange=“setTimeout(\'Hello!\'),500)”
这些功能不起作用,因此无需在中定义功能component@BhagwatTupe:
onchange
在全局上下文中工作,因为setTimeout在全局上下文中,它将工作。。。。。缺点:您不能直接在此处访问组件功能,谢谢@sandy,我在事件
(更改)
中犯了错误,这将是
onchange
<div class="form-group">
<label>Select Type</label>
<select 
    [name]="contact.contactMethod" 
    [id]="contact.contactMethod" 
    class="form-control"
    [(ngModel)]="contact.contactMethod">
    onchange="setTimeout('alert(\'Hello!\')', 5000)"
    <option *ngFor="let method of contactMethodsArray" [value]="method.id">{{ method.label }}</option>
</select>