Search code examples
javascriptangularjsfamo.usfamous-angular

How can you alter the classList of an element in Famo.us + Angular?


I'm using Famo.us + Angular. I'm able to retrieve the classList of a Surface by doing this:

$scope.findElement = function() {
    var elem = $famous.find("#colored-bg")[0].renderNode;  // Returns Surface
    console.log(elem.classList);  // RETURNS: ["purple-bg", "container-border", "box-shadow"]
};

You can't perform any of the operations on Famo.us object which you normal could to any other object on the DOM. For example, I thought I could add, remove, or replace classes, similar to this:

document.getElementById('id').classList.add('class');
document.getElementById('id').classList.remove('class'); 

add and remove do not exist, though. I can return the class list, and even individual items from the list (ex: Just the first class), but you cannot alter it. Any suggestions?


Solution

  • The setClasses method takes an array and you can set classes using:

    renderNode.setClasses(['white-bg', 'big-text']);
    

    Use addClass by passing the class name to add a class using:

    renderNode.addClass('big-text');
    

    Use removeClass by passing the class name to remove a class using:

    renderNode.removeClass('big-text');
    

    Use toggleClass by passing the class name to add/remove based on whether it exists:

    renderNode.toggleClass('big-text');