Javascript Angular.js getElementById()不在$scope函数中工作
el=document.getElementById(id);在以下函数内时不工作…el为空。在浏览器调试中,我可以使用相同的代码提取元素。我是Angular.js的新手。我能不能在附加到作用域的函数中使用常规javascriptJavascript Angular.js getElementById()不在$scope函数中工作,javascript,angularjs,Javascript,Angularjs,el=document.getElementById(id);在以下函数内时不工作…el为空。在浏览器调试中,我可以使用相同的代码提取元素。我是Angular.js的新手。我能不能在附加到作用域的函数中使用常规javascript myappApp.controller('Scroller', function($scope, $location, $anchorScroll) { $scope.scrollTo = function(id) { el = document.ge
myappApp.controller('Scroller', function($scope, $location, $anchorScroll) {
$scope.scrollTo = function(id) {
el = document.getElementById(id);
}
我想DOM还没有加载。因此,请确保getElementById()在DOM完全加载后运行。如果在“加载”事件触发后失败,则这是由其他原因导致的 HTML
document.getElementById不起作用,因为执行脚本时可能未加载dom。这就是为什么函数不返回任何内容。无论如何,这样做是没有用的。当您想要更改图元时,可以使用angular的指令或将图元绑定到angular模型。访问控制器中的元素是不好的做法,很少/从未需要。告诉我们你想做什么,我们可以帮你
<body ng-controller="sample">
<h1 id="foo">bar</h1>
</body>
var app = angular.module('myApp', []);
app.controller('sample', function($scope){
addEventListener('load', load, false);
function load(){
var el = document.getElementById("foo");
alert(el);
}
});