gpt4 book ai didi

AngularJs:数组推送替换其他元素

转载 作者:行者123 更新时间:2023-12-02 03:13:12 26 4
gpt4 key购买 nike

使用 ng-repeat 填充列表中的数据。

HTML:-

<div ng-repeat="data in GCAList|filter:year" class="listGCA" ng-click="select()">
<div class="listLibraryName">
{{ data.LibraryName }}
</div>
<div class="listProjects">
{{ data.Projects }}
</div>
<div class="listStatus">
{{ data.Status }}
</div>
<div class="listYear">
{{data.TaxYear}}
</div>
</div>

创建了一个临时变量并将表单中的值存储在其中,然后将其插入数组。现在推送时的问题是, $$hashkey:"object:81"是自动生成的,并且对于下一次推送,生成相同的哈希键并替换数组中先前插入的数据。我在以下位置使用了“按 $index 跟踪”:

ng-repeat="GCAList 中的数据|filter:year track by $index"通过 $index 添加轨道无助于避免替换

脚本文件:

var app = angular.module("Accounts", ['restangular', 'ngDialog', 'ngAnimate'])
var temp = {
"id": 0,
"LibraryName": "" ,
"Projects": 0,
"Status": "",
"TaxYear": 0
};

app.controller("accountsController", function($scope, ngDialog, RestRepository) {
$scope.showEGCA = false;
$scope.showGCA = true;
$scope.name = "";
RestRepository.getJson().then(function(response) {
$scope.dataList = response;
console.log($scope.dataList);
$scope.GCAList = $scope.dataList[0];
console.log($scope.GCAList);
$scope.EGCAList = $scope.dataList[1];
console.log($scope.EGCAList);
});
$scope.popOpen = function() {
ngDialog.open({
template: 'Pop Up.html',
scope: $scope,
controller: function($scope) {
$scope.cancelCOA = function() {
ngDialog.close();
};
$scope.createGCA = function() {
temp.id = $scope.GCAList.length + 1;
temp.LibraryName = $scope.name;
temp.Projects = 2;
temp.Status = "Inactive";
temp.TaxYear = $scope.taxYear;
console.log(temp);
$scope.GCAList.push(temp);
console.log($scope.GCAList);
ngDialog.close();
};
},
closeByDocument: false,
closeByEscape: false,
showClose: false,
});
};
$scope.gcaOpen = function() {
$scope.showGCA = !$scope.showGCA;
$scope.showEGCA = true;
};
$scope.egcaOpen = function() {
$scope.showEGCA = !$scope.showEGCA;
$scope.showGCA = true;
};
});

app.config(function(RestangularProvider) {
RestangularProvider.setBaseUrl('http://10.198.50.19:98/jsonData/');
});

app.factory("RestRepository", [
'Restangular', function(Restangular) {
return {
getJson: function() {
return Restangular.one('jsonData.json').get();
}
}
}
]);

最佳答案

原因是,由于您每次在函数调用中都使用上面创建的相同对象,因此您将获得相同的哈希键值,然后将相同的实例添加到数组中。删除,

   var temp ={
"id":0,
"LibraryName":"" ,
"Projects":0,
"Status":"",
"TaxYear":0
};

并将它放在您的 createGCA() 中。你的代码看起来像,

    $scope.createGCA = function () {
$scope.temp ={
"id":0,
"LibraryName":"" ,
"Projects":0,
"Status":"",
"TaxYear":0
};
$scope.temp.id=$scope.GCAList.length+1;
$scope.temp.LibraryName=$scope.name;
$scope.temp.Projects=2;
$scope.temp.Status="Inactive";
$scope.temp.TaxYear=$scope.taxYear;
console.log($scope.temp);
$scope.GCAList.push($scope.temp);
console.log($scope.GCAList);
ngDialog.close();
};

关于AngularJs:数组推送替换其他元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38760636/

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