简体   繁体   English

为什么将ngStyle指令声明为[]?

[英]Why the ngStyle directive is declared into the []?

I am an absolute beginner with Angular 2 and I have the following dount related the correct syntax of the ngStyle directive. 我是Angular 2的绝对初学者,并且我有以下与ngStyle指令的正确语法有关的问题

I have this example (that works fine): 我有这个例子(工作正常):

<p [ngStyle]="{backgroundColor: getColor()}">Server with ID {{ serverID }} is {{ getServerStatus() }}</p>

I know that, in this case, the ngStyle directive is adding something like to: 我知道,在这种情况下, ngStyle指令将添加以下内容:

style="background-color: green;"

at my HTML paragraph. 在我的HTML段落中。

My doubt is related the correct meaning of this syntax. 我的疑问与该语法的正确含义有关。 Why is it: 为什么:

[ngStyle]="{backgroundColor: getColor()}"

and not 并不是

ngStyle="{backgroundColor: getColor()}"

Why is it into the [...] ? 为什么要放入[...] What it exactly means? 到底是什么意思?

It's called property binding . 这称为属性绑定 With the brackets the compiler tries to evaluate the expression. 使用方括号,编译器将尝试对表达式求值。 Without it, you are just passing a string. 没有它,您只是传递一个字符串。

So with the brackets, you are passing a javascript object: 因此,使用方括号,您正在传递一个javascript对象:

{
    backgroundColor: getColor()
}

Whereby the compiler will call the getColor() method from the component to get the right color. 因此,编译器将从组件中调用getColor()方法以获取正确的颜色。

On the other hand, and going off topic here, but if you want to pass a string while using brackets, you should wrap them in single quotes: 另一方面,这里不讨论主题,但是如果您想在使用方括号时传递字符串,则应将其用单引号引起来:

<div [property]="'hiii'"></div>

Angular 2 has 3 types of directives: Angular 2具有3种类型的指令:

  1. Attribute directives. 属性指令。
  2. Structural directives. 结构指令。
  3. Components. 组件。

ngStyle is an attribute directive. ngStyle是属性指令。 And all attribute directive to which we need to pass/assign values are written inside square brackets. 我们需要向其传递/分配值的所有属性指令都写在方括号内。 The built-in NgStyle directive in the Template Syntax guide, for example, can change several element styles at the same time. 例如,“模板语法”指南中的内置NgStyle指令可以同时更改几种元素样式。

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

相关问题 为什么ngStyle不起作用 - why is ngStyle not working 如何将ngStyle传递给Angular中定义为元素的指令? - How to pass ngStyle to directive defined as element in Angular? Vue:为什么在商店中声明时计算属性需要 v-model 指令中的 .value 而不是在组件中声明时 - Vue: Why does computed property require .value in v-model directive when declared in a store but not when declared in the component 有没有一种聪明的方法来查找指令的声明位置? - Is there a smart way to find where the directive is declared? 为什么在$ rootScope中可以使用指令? - Why directive is avaible in $rootScope? 为什么指令没有执行 - Why is the directive not executed 当指令中已经给出作用域时,如何访问控制器中声明的函数 - How to access a function declared in controller when scope is already given in directive 如果声明了哈希,Chrome是否应该忽略unsafe-inline指令? - Is Chrome supposed to ignore the unsafe-inline directive if a hash is declared? 从核心模块声明和导出指令,但不触发Angular 7 - Directive declared and exported from core module but not triggering Angular 7 为什么在ComponentWillMount中声明的变量未定义? - Why is the variable declared in ComponentWillMount Undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM