What is the difference between `value` attribute and `ng-value` attributes in angularjs

后端 未结 1 502
悲&欢浪女
悲&欢浪女 2020-12-09 04:34

What is the difference between value and ng-value attributes in angularjs templates? If I use ng-if on the field using value attribute

1条回答
  •  自闭症患者
    2020-12-09 05:02

    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'"
    

    UPDATE:
    Starting with v1.6, ngValue will also set the value property of the element (in addition to the value attribute). It might not affect your usecase, but it's another difference worth keeping in mind.

    0 讨论(0)
提交回复
热议问题