简体   繁体   English

如何在AngularJS中使用getElementById

[英]How to use getElementById in AngularJS

I have this code in javascript: 我在javascript中有以下代码:

<script>
var recognition;
document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
    recognition = new SpeechRecognition();
    recognition.onresult = function(event) {
        if (event.results.length > 0) {
            q.value = event.results[0][0].transcript;
            q.form.submit();
        }
    }
}
</script>
<form action="http://www.example.com/search">
    <input type="search" id="q" name="q" size=60>
    <input type="button" value="Click to Speak" onclick="recognition.start()">
</form>

How do I use getElementById with AngularJS? 如何在AngularJS中使用getElementById The line q.value = event.results[0][0].transcript; q.value = event.results[0][0].transcript; is intended to insert what users input through speech recognition into an element q (which is used as a textbox). 旨在将用户通过语音识别输入的内容插入元素q (用作文本框)。 How do I do that in AngularJS? 如何在AngularJS中做到这一点?

To get an element by its id, class, attribute, etc. you can use the built-in angular.element , which is a jQuery lite version for DOM manipulation. 要通过其id,class,attribute等获取元素,可以使用内置的angular.element ,它是DOM操作的jQuery Lite版本。

If jQuery is available, angular.element is an alias for the jQuery function. 如果有jQuery ,则angular.elementjQuery函数的别名。 If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or jqLite. 如果没有jQuery,则angular.element委托Angular的内置jQuery子集,称为“ jQuery lite”jqLit​​e。

var q = angular.element('#q');
q.val(event.results[0][0].transcript);
q.focus();

You make use of the angular directive ng-model, add 您使用angular指令ng-model,添加

ng-model="speechtext"

to your textfield and in your code you define the scope variable speechtext: 到您的文本字段,并在您的代码中定义范围变量Speechtext:

$scope.speechtext = q //or just the variable where the result is hold

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM