gpt4 book ai didi

javascript - ng-repeat 拼接显示双

转载 作者:搜寻专家 更新时间:2023-11-01 04:29:55 26 4
gpt4 key购买 nike

出于某种原因,当我将一个对象拼接到我的 ng-repeat 数组中时,它会加倍我拼接的内容并隐藏数组中的最后一个对象。

但是,如果我单击切换隐藏并“刷新”ng-repeat,它会显示正确的数据。

有谁知道为什么会发生这种情况以及我可以做些什么来解决它?

angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.workflow = {
flow: [{
"id": "1334f68db820f664",
"step_number": 1,
"tasks": [{
"id": "1334f68e3f20f665"
}]
}, {
"id": "134967a5ba205f5b",
"step_number": 2,
"tasks": [{
"id": "134972c5b420e027"
}]
}, {
"id": "1334f68e7d209ae6",
"step_number": 3,
"tasks": [{
"id": "1334f68ef6209ae7"
}]
}]
};

$scope.insertStep = function() {

var insertStepIndex = 1,
task_data = {
"id": null,
"step_number": (insertStepIndex + 2),
"tasks": []
};

//go through each item in the array
$.each($scope.workflow.flow, function(index, step) {
//if the step number is greater then the place you want to insert it into, increase the step numbers
if (step.step_number > $scope.workflow.flow[insertStepIndex].step_number) step.step_number++;
});

$scope.workflow.flow.splice((insertStepIndex + 1), 0, task_data);

}

$scope.toggleHide = function() {
$scope.hide = !$scope.hide;
}


});
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.js"></script>
<div ng-app="app" ng-controller="ctrl">

<div ng-click="insertStep()">Insert Step</div>
<br />
<br />

<div ng-click="toggleHide()">Toggle Repeat</div>
<br />
<br />

<div ng-if="!hide">
<div ng-repeat="data in workflow.flow | orderBy:'+step_number'" ng-init="$stepIndex = workflow.flow.indexOf(data)">
{{ workflow.flow[$stepIndex].step_number }}
</div>
</div>
</div>

最佳答案

我遇到了问题。这是一个棘手但简单的部分。 ng-init 指令只执行一次。因此,当您将新数据推送到数组/列表时,您对 $stepIndex = workflow.flow.indexOf(data) 的分配不会更新。

因此添加作用域函数将解决此问题,因为 Angular 会在返回值更改时自动调用该函数。

angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.workflow = {
flow: [{
"id": "1334f68db820f664",
"step_number": 1,
"tasks": [{
"id": "1334f68e3f20f665"
}]
}, {
"id": "134967a5ba205f5b",
"step_number": 2,
"tasks": [{
"id": "134972c5b420e027"
}]
}, {
"id": "1334f68e7d209ae6",
"step_number": 3,
"tasks": [{
"id": "1334f68ef6209ae7"
}]
}]
};

$scope.insertStep = function() {

var insertStepIndex = 1
var task_data = {
"id": null,
"step_number": (insertStepIndex + 2),
"tasks": []
};

//go through each item in the array
angular.forEach($scope.workflow.flow, function(step, index) {
//if the step number is greater then the place you want to insert it into, increase the step numbers
if (step.step_number > $scope.workflow.flow[insertStepIndex].step_number) step.step_number++;
});

$scope.workflow.flow.splice((insertStepIndex + 1), 0, task_data);

}

// This is a new function which I added to fix the problem
$scope.getIndex = function(data) {
return $scope.workflow.flow.indexOf(data);
};

$scope.toggleHide = function() {
$scope.hide = !$scope.hide;
};

});
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.js"></script>
<div ng-app="app" ng-controller="ctrl">

<div ng-click="insertStep()">Insert Step</div>
<br />
<br />

<div ng-click="toggleHide()">Toggle Repeat</div>
<br />
<br />

<div ng-if="!hide">
<div ng-repeat="data in workflow.flow | orderBy:'+step_number'">
{{ workflow.flow[getIndex(data)].step_number }}
</div>
</div>
</div>

关于javascript - ng-repeat 拼接显示双,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987714/

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