Angular.js的getElementById()在$范围功能无法正常工作无法正常、范围、功能、工作

由网友(我向来冰冷成僻却想你好久)分享简介:EL =的document.getElementById(id)的;不工作时,下面的函数内... el的为空。在浏览器的调试,我可以拉起来用相同的code中的元素。我是新来Angular.js。我能不能连接到范围函数中使用常规的JavaScript? myappApp.controller('滚轮',函数($范围...

EL =的document.getElementById(id)的;不工作时,下面的函数内... el的为空。在浏览器的调试,我可以拉起来用相同的code中的元素。我是新来Angular.js。我能不能连接到范围函数中使用常规的JavaScript?

  myappApp.controller('滚轮',函数($范围,$位置$ anchorScroll){    $ scope.scrollTo =功能(ID){    EL =的document.getElementById(id)的;    } 

解决方案

我觉得DOM没有加载呢。所以请确保的getElementById()运行DOM完全加载后。如果你的负荷事件触发后的失败,这是源于另一个原因。

HTML

 <机身NG控制器=样品>    < H1 ID =foo的>巴≤; / H1>< /身体GT; 

JS

  VAR应用= angular.module('对myApp',[]);    app.controller('样品',函数($范围){        的addEventListener('负荷',负载,FALSE);        功能负荷(){            VAR EL =的document.getElementById(富);            警报(EL);        }    }); 
如何在angularjs NG

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);
        }

    });

阅读全文

相关推荐

最新文章