是什么value`属性和`在angularjs NG-value`属性之间`的区别 [英] What is the difference between `value` attribute and `ng-value` attributes in angularjs

查看:1237
本文介绍了是什么value`属性和`在angularjs NG-value`属性之间`的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 NG-值在angularjs模板属性之间的区别?如果我用值中使用 NG-IF 场上属性它工作正常,但如果我更改属性 NG-值将停止工作。

 例如1 //它的工作原理<输入类型='无线电'NG-模式='困难'值='硬'/>
< D​​IV NG-IF =难度=='硬'>
     &所述p为H.;困难是硬下; / P>
< / DIV>例2 //这是行不通的<输入类型='无线电'NG-模式='级别'NG值='硬'/>
< D​​IV NG-IF =级=='硬'>
     &所述; P>级是硬下; / P>
< / DIV>


解决方案

的文档 ngValue 需要一个的角前pression,其价值将被绑定到输入元素的属性

所以,当你使用 NG-值=硬,这是PTED作为一个前pression和值势必 $ scope.hard (这可能是未定义)< BR>
ngValue 是评估前pressions有用 - 它没有优势设置硬codeD值。然而,如果你想用硬 ngValue codeA值,则必须将其括在

  NG-值='硬'

What is the difference between value and ng-value attributes in angularjs templates? If I use ng-if on the field using value attribute it works properly but if I change the attribute value to ng-value it stops working.

example 1  // it works 

<input type='radio' ng-model='difficulty' value='hard'/>
<div ng-if="difficulty == 'hard'">
     <p>difficulty is hard</p>
</div>  

Example 2 // it doesn't work

<input type='radio' ng-model='level' ng-value='hard'/>
<div ng-if= "level == 'hard'" >
     <p>level is hard</p>
</div>

According to the docs, ngValue takes an "angular expression, whose value will be bound to the value attribute of the input element".

So, when you use ng-value="hard", it is interpreted as an expression and the value is bound to $scope.hard (which is probably undefined).
ngValue is useful for evaluating expressions - it has no advantage over value for setting hard-coded values. Yet, if you want to hard-code a value with ngValue, you must enclose it in '':

ng-value="'hard'"

这篇关于是什么value`属性和`在angularjs NG-value`属性之间`的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆