rolory
rolory

Reputation: 362

Angular JS ng-if

I'm new to AngularJS and I'm learning it independently.
I read about ng-if using the official AngularJS website.

I got an exercise which I need to scan a color, and if the color is white (#fff in hex), I should change the span's background-color to black (#000 in hex).

This is what I've tried JSbin.
I involved Javascript because I don't know how to deal with CSS on Angular and I think that's they reason why the code is not working as expected.

Sorry for being ignorant.

Thanks in advance.

Upvotes: 0

Views: 53

Answers (1)

Mosho
Mosho

Reputation: 7078

You don't use inline JS (onclick) when you have ng-click (or at all, for that matter). I suggest you take a step back and make sure you understand what's going on, and not just put in random bits of code in hope they would give you what you want.

Here's an updated demo.

First, I removed the onClick. then I changed the function to be included in the scope as that's the only way things you put in things like ng-click can find those functions. Last, I changed added a backgroundColor variable to be set to the desired color when the button is clicked, and changed the style attribute of the output element to have a background-color rather than color like you had.

Also, I don't see how ng-if has anything to do with this.

Upvotes: 1

Related Questions