gpt4 book ai didi

javascript - AngularJS querySelector 按 ID 返回未定义

转载 作者:行者123 更新时间:2023-12-03 08:24:44 25 4
gpt4 key购买 nike

这是我的 index.html 的样子:

enter image description here

这是 Javascript 代码( Angular ):

var controllerElement = document.querySelector('[id="tile3"]');
console.log(controllerElement.getAttribute('class'));
var controllerScope = angular.element(controllerElement).scope();

如您所见,我尝试通过搜索等于 tile3 的 id 来查找controllerElement。但是,每当我到达下一行时,程序就会崩溃并出现以下错误:

Uncaught TypeError: Cannot read property 'getAttribute' of null

我是否遗漏了一些明显的东西?

编辑

这是完整的代码,现在 controllerScope var 由于某种原因未定义......

var update = function(id, channel){
var controllerElement = document.querySelector('#tile3');
console.log(controllerElement.getAttribute('ng-controller'));
var controllerScope = angular.element(controllerElement).scope();
console.log(controllerScope);
controllerScope.$apply(function () {
controllerScope[id].username = channel.username;
controllerScope[id].keyword = channel.keyword;
controllerScope[id].percent = channel.percent;
controllerScope[id].views = channel.views;
controllerScope[id].link = channel.link;
});
};

(function(){
var app = angular.module("testApp", []);

var TileController = function($scope){
$scope.channels = [];
for(i = 0; i < 25; i++){
var channel = {
username:"John",
keyword:"Doe",
percent:"50%",
views:5000,
link:"http://www.twitch.tv/lolyou"
};
$scope.channels.push(channel);
}

};

app.controller("TileController", ["$scope", TileController]);

update(3, {username:"Yo",
keyword:"Bro",
percent:"40%",
views:35,
link:"http://www.twitch.tv/nickbunyun"});
})();

其中显示 console.log(controllerScope); 的行只是打印“未定义”。

最佳答案

如果您使用querySelector,那么您可以/应该只使用#tile3 作为传递给函数的值,因此:

vartile3 = document.querySelector('#tile3')

关于javascript - AngularJS querySelector 按 ID 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33601064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com