©
This document uses PHP Chinese website manual Release
绑定给定的表达式到input[select]
或 input[radio]
的值上,这样当元素被选中时, 元素的ngModel
会被设为当前绑定的值。
ngValue
经常用于使用ng-repeat
动态生成单选按钮列表,如下面所示。
<input
[ng-value=""]>
...
</input>
Param | 类型 | 详述 |
---|---|---|
ngValue
(可选)
|
string | Angular表达式,绑定到 |
<script>
angular.module('valueExample', [])
.controller('ExampleController', ['$scope', Function($scope) {
$scope.names = ['pizza', 'unicorns', 'robots'];
$scope.my = { favorite: 'unicorns' };
}]);
</script>
<form ng-controller="ExampleController">
<h2>Which is your favorite?</h2>
<label ng-repeat="name in names" for="{{name}}">
{{name}}
<input Type="radio"
ng-model="my.favorite"
ng-value="name"
id="{{name}}"
name="favorite">
</label>
<div>You chose {{my.favorite}}</div>
</form>
var favorite = element(by.binding('my.favorite'));
it('should initialize to model', Function() {
expect(favorite.getText()).toContain('unicorns');});
it('should bind the values to the inputs', Function() {
element.all(by.model('my.favorite')).get(0).click();
expect(favorite.getText()).toContain('pizza');});