angularjs getElementById null
document.getElementById doenst work cause the dom is maybe not loaded when the script is executed. Thats why the function retuns nothing. Anyway: There is no use in doing that. When you wanna change the element you can use directives from angular or bind the element to an angular-model. Accessing the element in the controller is bad practice and rarely/never needed. Just tell us what you wanna do and we may help you
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.
<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);
}
});
document.getElementById doenst work cause the dom is maybe not loaded when the script is executed. Thats why the function retuns nothing. Anyway: There is no use in doing that. When you wanna change the element you can use directives from angular or bind the element to an angular-model. Accessing the element in the controller is bad practice and rarely/never needed. Just tell us what you wanna do and we may help you
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.
<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);
}
});
尊贵的董事大人
英文标题不为空时 视为本栏投稿
需要关键字 描述 英文标题