javascript - Angular.js getElementById() not working in $scope function -
el = document.getelementbyid(id); not working when inside below function...the el null. in browser debug can pull element same code. new angular.js. can not use regular javascript in function attached scope?
myappapp.controller('scroller', function($scope, $location, $anchorscroll) { $scope.scrollto = function(id) { el = document.getelementbyid(id); }
i think dom not loaded yet. please make sure getelementbyid() run after dom loaded completely. if fail after 'load' event fires, resulted 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); } });
Comments
Post a Comment