el = document.getElementById(id); is not working when inside the below function...the el is null. In the browser debug I can pull up the element with the same code. I am new to Angular.js. Can I not use regular javascript in a function attached to the scope?
myappApp.controller('Scroller', function($scope, $location, $anchorScroll) {
$scope.scrollTo = function(id) {
el = document.getElementById(id);
}
I think DOM is not loaded yet. So please make sure getElementById() run after DOM is loaded completely. If you fail after the 'load' event fires, this is resulted from another cause.
HTML
<body ng-controller="sample">
<h1 id="foo">bar</h1>
</body>
JS
var app = angular.module('myApp', []);
app.controller('sample', function($scope){
addEventListener('load', load, false);
function load(){
var el = document.getElementById("foo");
alert(el);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With