使用Angular中继器警告多个输入的输入值
Alert input value of multiple inputs using an Angular repeater
我正在使用Angular来允许我创建尽可能多的输入,但我想有一个简单的警报,将显示输入的值。我已经能够得到这个值,如果我不使用repeat,但不是在我有repeat之后。
<div ng-controller="MainCtrl">
<form id="quickPick-form1" class="list" ng-submit="submit()" >
<fieldset data-ng-repeat="choice in choices" class="clearfix">
<input type="text" placeholder="Item" class="pull-left" ng-model="item">
<button class="remove pull-left" ng-show="$last" ng-click="removeChoice()">X</button>
</fieldset>
<button id="quickPick-button1" style="font-weight:600;font-style:italic;" class="addfields button button-calm button-block button-outline " ng-click="addNewChoice()">Tap me to add an item!</button>
<div class="spacer" style="width: 300px; height: 40px;"></div>
<p>Have enough items added? Click Randomize</p>
<button ng-click="showChoice()" id="quickPick-button2" class=" button button-calm button-block ">Randomize</button>
<div class="spacer" style="width: 300px; height: 20px;"></div>
</form>
</div>
<script>
.controller('MainCtrl', function($scope) {
$scope.choices = [{id: 'choice1'}, {id: 'choice2'}];
$scope.addNewChoice = function() {
var newItemNo = $scope.choices.length+1;
$scope.choices.push({'id':'choice'+newItemNo});
};
$scope.removeChoice = function() {
var lastItem = $scope.choices.length-1;
$scope.choices.splice(lastItem);
};
$scope.item = null;
$scope.showChoice = function(){
alert($scope.item);
};
});
</script>
试试这个
$scope.showChoice = function(){
alert($scope.choices.split(" /n"));
};
item
不在您的作用域内。您的作用域中只有choices[]
。
这是一个Plunker。您需要将您的模型更改为choice.item。
<form id="quickPick-form1" class="list" ng-submit="submit()" >
<fieldset data-ng-repeat="choice in choices" class="clearfix">
<input type="text" placeholder="Item" class="pull-left" ng-model="choice.item">
<button class="remove pull-left" ng-show="$last" ng-click="removeChoice()">X</button>
</fieldset>
<button id="quickPick-button1" style="font-weight:600;font-style:italic;" class="addfields button button-calm button-block button-outline " ng-click="addNewChoice()">Tap me to add an item!</button>
<div class="spacer" style="width: 300px; height: 40px;"></div>
<p>Have enough items added? Click Randomize</p>
<button ng-click="showChoice()" id="quickPick-button2" class=" button button-calm button-block ">Randomize</button>
<div class="spacer" style="width: 300px; height: 20px;"></div>
和在JavaScript中:
$scope.showChoice = function(){
var myChoices = '';
for(var i =0; i < $scope.choices.length; i++){
myChoices = myChoices + ', ' + $scope.choices[i].item;
}
alert(myChoices)
};
相关文章:
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 将输入值设置为ng模型属性[Angular]时出现问题
- 将日期添加到日期输入字段Angular
- 当输入字段为空时,如何在angular中调用方法
- 如何在angular bootstrap ui中聚焦活动导航选项卡中的拳头输入字段
- Angular指令中的最佳实践是将文本输入设置为英尺和英寸的格式
- 触发器<输入类型=“;文件“>用angular程序选择文件
- 将脚本应用于Angular 2上的输入
- 如何通过Angular中的$resource发送来自自定义指令的输入值
- 提前输入以在 Angular 模板中工作
- 如何使用 Angular JS 聚焦输入
- 为什么Angular ng消息'当=“0”时;“必需”'即使输入字段有输入,也会激发
- 控制器内部无法访问Angular js输入模型
- json数据验证,使用angular Js和javascript进行输入值验证
- 如何从html输入类型=“”中只获得Angular控制器变量中的时间值;时间”;
- 使用Angular.js验证表单输入的原始状态
- Angular 2-模型驱动的表单,带有输入文件(文件上传)
- angular.js将jquery自动完成对象绑定到ng模型,将值输入到对象的属性
- 我需要一个使用Angular.js的输入框,它的值取决于一个公式
- 在动态创建的文本框中动态添加整数作为用户输入-Angular JS