0

I'm looking for an example to change CSS class of an HTML element from inside the logic of the AngularJS controller

In the controller, something like this:

function myFunc() {
  //do a calculation
  //result is 3
  //change the class of HTML element with id="3" to add class="hidden"

I can't do it with ngClick because the controller decides which element to change, not the user.

Like if I wanted to recreate that Simon game from the 80's I would have the controller pick a random number 1-4 and then light up the HTML element and the user would have to click it in a certain amount of time.

choice = Math.floor(Math.random() * 4) + 1;
if ( choice == 2 ) {
  lightUpBlue();
  setTimeout(turnOffBlue();, 2000);
}

How would I write lightUpBlue() to change the color of a button in the HTML document, and maybe change the hyperlink or some other property?

Thanks in advance.

1
  • It's hard for me to pick the right answer. Shashank's answer is working for me now but I am starting to think Luba's might suit me better but I haven't been able to implement it yet. Commented Jan 1, 2015 at 14:38

3 Answers 3

1

Use ng-class inside your html code.

HTML for Elements

ng-class="myFunction"

Documentation for ng-class

Controller

scope.myFunction = function() {
}

Documentation for controller
Documentation for scopes

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

8 Comments

I don't think that will work the way I want it. It would require the change to be initiated by from the HTML, the front end. I need it to initiate from the controller, back end. My understanding may be wrong though.
The controller is on the frontend, too. Using AngularJS.
Well yeah it's on the client side. So far I have been thinking of it like client-side Back-end because it practically talks to the DB directly. You can even stick a mini static DB in there in the form of an object. I have a hard time calling the controller "front-end".
You can use an isomorphic solution. There is a controller backend, too.
Where can I find more info about the it/by/ps functions? I'm trying to make your answer work. If your answer does what I think it might do it may be a more flexible solution.
|
1

Best practice is to avoid manipulating DOM element from inside controllers or services. For this you can write a custom directive.

Well you can modify any of the element like:

angular.element(document.querySelector('[id="3"]')).addClass('hidden');

2 Comments

Why should one avoid manipulating DOM from the controller? I'm not familiar with the terminology "custom directive", is that what your example is? Where do I put it if not the controller?
Because the business logic & view logic needs to be separated and that is what angular is knows as an MVC architecture i.e why we should not do the DOM manipulation in controllers. Angular provides various directive and we can create our own directives to do more. Go with the link docs.angularjs.org/guide/directive and take a high overview, you will get the gist.
0

ngShow Looks like the best solution for hiding an element.

Comments

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.