动态追加值到数组angularjs
append dynamically value into array angularjs
$scope.results = [
{id: 1, text: 'a'},
{id: 2, text: 'b'},
{id: 3, text: 'c'},
{id: 4, text: 'd'}
];
<label ng-repeat="result in results">
<input type="checkbox" name="res" data-checklist-model="my.resid" data-checklist-value="result.id" ng-click="myFunction(result.id)" > {{result.text}}
</label>
$scope.myFunction = function(id){
$scope.dflt = [];
if($scope.my.resid == 1){
$scope.dflt.push({"id": 1, "text": 'a'});
console.log($scope.dflt);
}
}
我想动态地添加我期望的结果,但现在它只是显示[{"id":1,"text":"a"}]
[{"id":1,"text":"a"}, {"id":2,"text":"b"}, {"id":3,"text":"c"}, {"id":4,"text":"d"}]
每次点击,$scope.dflt
再次初始化…
var myApp = angular.module("myApp", []);
myApp.controller("myCtrl", function($scope) {
$scope.results = [{
id: 1,
text: 'a'
}, {
id: 2,
text: 'b'
}, {
id: 3,
text: 'c'
}, {
id: 4,
text: 'd'
}];
$scope.dflt = [];
$scope.myFunction = function(obj) {
$scope.dflt.push(obj);
console.log($scope.dflt);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<label ng-repeat="result in results">
<input type="checkbox" name="res" data-checklist-model="my.resid" data-checklist-value="result.id" ng-click="myFunction(result)">{{result.id}}
</label>
</div>
$scope.results = [
{id: 1, text: 'a'},
{id: 2, text: 'b'},
{id: 3, text: 'c'},
{id: 4, text: 'd'}
];
<label ng-repeat="result in results">
<input type="checkbox" name="res" data-checklist-model="my.resid" data-checklist-value="result.id" ng-click="myFunction(result.id)" > {{result.text}}
</label>
$scope.myFunction = function(id){
$scope.dflt = $scope.dflt || [];
if($scope.my.resid == 1){
$scope.dflt.push({"id": 1, "text": 'a'});
console.log($scope.dflt);
}
}
Make $scope。在当前函数之外的DFLT,它会工作。每次执行单击
相关文章:
- 如何将angularjs中的javascript字符串输出为循环数组
- angularjs$watch获取已更改数组项的索引
- AngularJS-ng使用字符串索引在数组上重复
- 如何使用angularJS从json的对象数组中显示和计数实体
- angularjs ng基于点击时的数组值重复过滤器
- 使用angularjs数组中的slice
- 从空格分隔的文本创建和实现数组:AngularJS
- json数组angularjs中的自定义过滤器
- 在方法中看不到对象数组.(Angularjs)
- 查找对象数组(Angularjs)中是否存在值
- $scope绑定到空值数组 AngularJs
- 同一数组(AngularJS)中的javascript对象之间的一对一关系
- 编译和传递数组 - AngularJS和PHP
- 如何根据数组AngularJS中的元素更改href
- 通过id过滤数组angularjs的数组
- 查找数组angularjs对象的索引
- 在数组angularjs javascript中选中或取消选中复选框
- 将JSON响应存储在数组angularjs中
- 如何将元素推送到指令中的数组 - AngularJS
- 动态追加值到数组angularjs