gpt4 book ai didi

javascript - 将json数据推送到angular js中的现有数组

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:03 24 4
gpt4 key购买 nike

我在将数据推送到现有数组时遇到问题。您可以看到我正在将数据发布到表中,但是,当用户输入 8 位条形码时,我喜欢将数据推送到表中。

enter image description here

工厂

    angular.module('app.pickUpServ', []).factory('pickUpServ', ['$rootScope', '$http',
function($rootScope, $http) {
return {
getPickUpList: function(data) {
$http({
method: 'POST',
url: 'app/Service/CourierService.asmx/BarcodeList',
data: {
"bardcodeVal": "",
"courierType": "PICKUP",
"userName": "aspuser"
},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
}).success(data).error(function(error) {
console.log('Error - getPickUpList');
});
},
items: [{
"bardcodeVal": "",
"courierType": "PICKUP",
"userName": "aspuser"
}],
add: function(item) {
this.items.push(item);
console.log(item);
}
};
}
]);

Controller

angular.module('app.scanListCtrl', []).controller('ScanListCtrl', ['$scope', 'pickUpServ',
function ($scope, pickUpServ) {
//Get Pick Up Data
if ($scope.title == 'Pick Up') {

pickUpServ.getPickUpList(function (data) {
$scope.items = data.d
});

$scope.autoAddItem = function () {
if (($scope.BarcodeValue + '').length == 8) {
pickUpServ.add({
"barcodeVal": $scope.BarcodeValue,
"courierType": "PICKUP",
"userName": "aspuser"
});
$scope.BarcodeValue = "";
}
};
}
}
]);

HTML

<div ng-controller="ScanListCtrl">
<div class="row prepend-top-md">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<i class="fa fa-barcode"></i>&nbspScan Item</h3>
</div>
<div class="panel-body">
<div class="input-group input-group-lg">
<input type="number" class="form-control" placeholder="Scan Item" ng-model="BarcodeValue"
ng-change="autoAddItem()" is-focus>
<span class="input-group-btn">
<button class="btn btn-info" type="button" ng-click="addRow()">
Add Barcode</button>
</span></div>
</div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th class="text-center" style="width: 3%">
#
</th>
<th>
<i class="fa fa-barcode"></i>&nbspBarcode
</th>
<th>
<i class="fa fa-medkit"></i>&nbspCSN
</th>
<th>
<i class="fa fa-user"></i>&nbspUser
</th>
<th>
<i class="fa fa-clock-o"></i>&nbspDate
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items | orderBy:'Id':true:reverse">
<td class="text-center">
[{{item.Id}}]
</td>
<td>
{{item.BarcodeValue}}
</td>
<td>
{{item.CSN}}
</td>
<td>
{{item.LastName + ', ' + item.FirstName}}
</td>
<td>
{{item.Created}}
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

最佳答案

您要将新项目添加到作用域外(工厂内)的其他元素,必须执行如下操作:

        $scope.autoAddItem = function () {
if (($scope.BarcodeValue + '').length == 8) {
$scope.items.push({
"barcodeVal": $scope.BarcodeValue,
"courierType": "PICKUP",
"userName": "aspuser"
});

$scope.BarcodeValue = "";
}
};

如果你想 make all inside the factory 必须是这样的(忽略上面的变化):

angular.module('app.pickUpServ', []).factory('pickUpServ', ['$rootScope', '$http',
function($rootScope, $http) {
return {
getPickUpList: function(callback) {
var _this = this;
$http({
method: 'POST',
url: 'app/Service/CourierService.asmx/BarcodeList',
data: {
"bardcodeVal": "",
"courierType": "PICKUP",
"userName": "aspuser"
},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
})
.success(function(data) {
_this.items = data.d;
callback(_this.items) //This gonna set to $scope the items in factory and angular
//link the object items to $scope.items (Code not tested but must work)
})
.error(function(error) {
console.log('Error - getPickUpList');
});
},
items: [{
"bardcodeVal": "",
"courierType": "PICKUP",
"userName": "aspuser"
}],
add: function(item) {
this.items.push(item);
console.log(item);
}
};
}
]);

关于javascript - 将json数据推送到angular js中的现有数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25365378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com