简体   繁体   English

如何在 AngularJS 中访问 DOM 元素

[英]How to access DOM element in AngularJS

I'm new to AngularJS.我是 AngularJS 的新手。 I have an app with basic routing etc but I'd like to add a script that will add some interactivity to my home screen.我有一个具有基本路由等功能的应用程序,但我想添加一个脚本来为我的主屏幕添加一些交互性。 This is a typewriter js script:这是一个打字机js脚本:

const texts = ['123', '456', '789'];
let count = 0;
let index = 0;
let currentText = '';
let letter = '';

(function type() {
  if (count === texts.length) {
    count = 0;
  }
  currentText = texts[count];
  letter = currentText.slice(0, ++index);

  document.querySelector(".typing").innerHTML = letter;
  angular.element(document.querySelector('.typing'));

  if (letter.length === currentText.length) {
    count++;
    index = 0;
  }
  setTimeout(type, 400);
})();

This works locally but when I deploy to Heroku it says:这在本地有效,但是当我部署到 Heroku 时,它说:

Uncaught TypeError: Cannot set property 'innerHTML' of null未捕获的类型错误:无法设置 null 的属性“innerHTML”

This tells me that the script executes before the homepage view is loaded.这告诉我脚本在加载主页视图之前执行。 What's the best solution for this.什么是最好的解决方案。 Can I stick this code inside app.js and if so, how do I do that?我可以将此代码粘贴到 app.js 中吗?如果可以,我该怎么做?

I have already tried to wrap my code inside window.onload and it didn't make any difference.我已经尝试将我的代码包装在 window.onload 中,但没有任何区别。

Firstly, In angularjs scripts are wrapped inside a controller/service/factories which should be registered in your app module.首先,在 angularjs 脚本中,应该在您的应用程序模块中注册一个控制器/服务/工厂。

Check the below tutorial https://www.w3schools.com/angular/angular_controllers.asp检查以下教程https://www.w3schools.com/angular/angular_controllers.asp

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

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