0

Here is what my index.html looks like:

enter image description here

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".

9
  • Where is this code?The queryselector. Commented Nov 9, 2015 at 1:20
  • What are you talking about? The line where it says document.querySelector(...);? The error is being thrown on the second line. Commented Nov 9, 2015 at 1:27
  • Yeah is it in a controller or something Commented Nov 9, 2015 at 1:27
  • What do you mean "in a controller or something"? You can see the code I've provided. I think I've isolated the problem. Commented Nov 9, 2015 at 1:33
  • Just give us more code :) Commented Nov 9, 2015 at 1:41

1 Answer 1

1

If you are using querySelector then you could/should just use #tile3 as value passed to the function, so:

var tile3 = document.querySelector('#tile3')

Sign up to request clarification or add additional context in comments.

3 Comments

var controllerElement = document.querySelector('#tile3'); That code still throws the same error.
I believe you need to post the whole JavaScript code you wrote. It seems like the div is not created when you try to get it, therefore it's null.
awww, I had the script created in the head of my index file so it was being called before the body was created I guess. I just moved the script tags to the bottom of the body but I'm getting a new error: "Uncaught TypeError: Cannot read property '$apply' of undefined" which is weird considering that it successfully printed out the class attr of the element meaning that the element is defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.