?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
綁定給定的表達式到input[select]
或 input[radio]
的值上,這樣當元素被選中時, 元素的ngModel
會被設(shè)為當前綁定的值。
ngValue
經(jīng)常用于使用ng-repeat
動態(tài)生成單選按鈕列表,如下面所示。
<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');});