简体   繁体   English

角JS ng-if

[英]Angular JS ng-if

I'm new to AngularJS and I'm learning it independently. 我是AngularJS的新手,并且正在独立学习。
I read about ng-if using the official AngularJS website . 使用AngularJS官方网站了解了ng-if

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). 我进行了一项练习,需要扫描一种颜色,如果颜色是白色 (十六进制为#fff ),则应将跨度的背景色更改为黑色 (十六进制为#000 )。

This is what I've tried JSbin . 这就是我尝试过的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. 我参与了Javascript,因为我不知道如何在Angular上处理CSS,我认为这就是为什么代码无法按预期工作的原因。

Sorry for being ignorant. 对不起,我很无知。

Thanks in advance. 提前致谢。

You don't use inline JS ( onclick ) when you have ng-click (or at all, for that matter). 进行ng-click (或完全不这样做),您无需使用内联JS( onclick )。 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 . 首先,我删除了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. 然后我更改了要包含在scope的函数,因为这是您放入ng-click类的东西才能找到这些函数的唯一方法。 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. 最后,我更改了添加的backgroundColor变量,以在单击按钮时将其设置为所需的颜色,并将输出元素的style属性更改为具有background-color而不是像以前那样具有color

Also, I don't see how ng-if has anything to do with this. 另外,我看不到ng-if与这有什么关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM