【发布时间】:2014-10-11 11:43:56
【问题描述】:
我在将数据推送到现有数组时遇到问题。您可以看到我正在将数据发布到表格中,但是,当用户输入 8 位条形码时,我喜欢将数据推送到表格中。
工厂
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);
}
};
}
]);
控制器
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> Scan 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> Barcode
</th>
<th>
<i class="fa fa-medkit"></i> CSN
</th>
<th>
<i class="fa fa-user"></i> User
</th>
<th>
<i class="fa fa-clock-o"></i> Date
</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.items两次。您必须对pickUpServ.items的原始引用被替换为从 POST 请求返回的新项目数组。这是故意的吗? -
谢谢。我理解了。这是一个小错误。但我仍然在将数据推送到表中。
-
我建议您在服务/工厂中执行所有数组操作。这样你只需调用 pickUpServ.add() 然后在路由的 resolve 方法中使用 pickUpServ.get()。
-
@CorySilva - 谢谢你的建议。我喜欢尝试使用控制器中的 add()。
标签: javascript html json angularjs