简体   繁体   English

如何仅在 textarea 完全加载后运行 controller function?

[英]How can I run a controller function only after textarea fully loaded?

I have a textarea like below:我有一个如下的文本区域:

<textarea rows="3" maxlength="144" ng-maxlength="144" type="text"
          name="testPost" id="testPost_{{item.id}}"
          ng-init="focusText('testPost', item.id)"
          ng-model="item.testPost">
</textarea>

with a function like below:使用 function 如下所示:

$scope.focusText = function (columnSelected, id){
    if(columnSelected == "testPost"){
        var textId = "testPost_" + id;
        document.getElementById(textId).focus();
        document.getElementById(textId).setSelectionRange(0,0);
    } 
}

The problem is, document.getElementById(textId) is null as function is ran before the textarea is loaded.问题是, document.getElementById(textId)null因为 function 在加载 textarea 之前运行。 How can I run the function only after textarea is loaded?如何仅在加载 textarea 后运行 function?

The problem is, document.getElementById(textId) is null问题是, document.getElementById(textId)null

Create a custom directive:创建自定义指令:

app.directive("myInit", function() {
   return {
       link: postLink
   };
   function postLink(scope, element, attrs)
       scope.$eval(attrs.myInit, {$element: element})
   }
})

Then pass the $element local to the function:然后将$element local 传递给 function:

<textarea rows="3" maxlength="144" ng-maxlength="144" type="text"
          name="testPost" id="testPost_{{item.id}}"
          my-init="focusText('testPost', item.id, $element)"
          ng-model="item.testPost">
</textarea>

Use the $element parameter in the focusText function:focusText function中使用$element参数:

$scope.focusText = function (columnSelected, id, $element){
    if(columnSelected == "testPost"){
        $element[0].focus();
        $element[0].setSelectionRange(0,0);
    } 
}

This avoids using document.getElementById and the error.这避免了使用document.getElementById和错误。

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

相关问题 如何仅在 Vue 完全加载和初始化后运行 VueJS 代码? - How to run VueJS code only after Vue is fully loaded and initialized? 页面和所有扩展程序完全加载后,如何运行javascript? - How can I run javascript after the page and all extensions are fully loaded? 内部脚本完全加载后如何运行脚本? - How do I run a script after internal scripts are fully loaded? 仅在页面完全加载并跳到垂直偏移位置后,才如何在浏览器中显示页面? - How can I display a page in the browser, only after it has fully loaded AND jumped to a vertical offset position? 页面完全加载后如何修复加载 gif? - How can i fix the loading gif after the page is fully loaded? 页面内容完全加载后如何获取? - How can I get a pages content after it has fully loaded? 在此get函数完全完成并呈现之后,如何确保运行其他函数? - How can I make sure that other functions is run after this get function is fully completed and rendered? EmberJS组件-在DOM完全加载后运行自定义函数 - EmberJS Component - Run custom function after DOM has fully loaded 在页面上的所有脚本完全加载后运行 js 函数 - run js function after all scripts on page are fully loaded 如果我链接外部javascript,在dom完全加载后是否可以运行? - If I link external javascript, does it run after the dom is fully loaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM