Here is what my index.html looks like:
And this is the Javascript code (angular):
var controllerElement = document.querySelector('[id="tile3"]');
console.log(controllerElement.getAttribute('class'));
var controllerScope = angular.element(controllerElement).scope();
As you can see, I'm trying to find the controllerElement by searching for an id equal to tile3
. However, whenever I get to the next line the program crashes with this error:
Uncaught TypeError: Cannot read property 'getAttribute' of null
Is there something obvious I'm missing?
EDIT
Here is the full code, now the controllerScope
var is being undefined for some reason...
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"});
})();
The line where it says console.log(controllerScope);
is just printing "undefined".