【发布时间】:2016-12-23 09:54:02
【问题描述】:
我有以下ng-repeat 工作正常。
<div ng-repeat="data in workflow.flow | orderBy:'+step_number'" ng-init="$stepIndex = workflow.flow.indexOf(data)">
{{ workflow.flow[$stepIndex].step_number }}
</div>
$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" } ]
}
]
这是它在我的 html 中的显示方式:
1
2
3
我有以下函数可以在数组中间添加一个步骤:
$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);
}
由于某种原因,ID 为 1334f68e7d209ae6 的数组 get 更改为 step_number: 4,但在 ng-repeat 中完全忽略了 get
这是显示的内容:
1
2
3
3
当我 console.log $scope.workflow.flow 这就是我所看到的:
[
{
"id":"1334f68db820f664",
"step_number":1,
"tasks":[ { "id":"1334f68e3f20f665" } ]
},
{
"id":"134967a5ba205f5b",
"step_number":2,
"tasks":[ { "id":"134972c5b420e027" } ]
},
{
"id":null,
"step_number":3,
"tasks":[]
},
{
"id":"1334f68e7d209ae6",
"step_number":4,
"tasks":[ { "id":"1334f68ef6209ae7" } ]
}
]
有人知道为什么会这样吗?
下面是代码sn-p:
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);
console.log($scope.workflow.flow);
}
});
<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-repeat="data in workflow.flow | orderBy:'+step_number'" ng-init="$stepIndex = workflow.flow.indexOf(data)">
{{ workflow.flow[$stepIndex].step_number }}
</div>
</div>
【问题讨论】:
-
你能在这里创建一个sn-p
-
@iceman 我已将其添加到问题中
标签: javascript angularjs angularjs-ng-repeat angularjs-orderby