在AngularJS中编辑JSON响应并绑定到列表
您好,我知道如何读取表单json并将输出绑定到视图,但是我想在输出和bid转换数据中添加一些逻辑。如何将我的forEach输出到数组中,然后基于它进行ng repeat,或者将我的ajax数据绑定与我的修改结合起来 现在如果我改变 $scope.fleet=newData;=>$scope.fleet=数据 和view.html例如{item.name}一切正常,但我想在绑定之前对名称进行一些更改 我的代码: controller.js在AngularJS中编辑JSON响应并绑定到列表,json,angularjs,angularjs-ng-repeat,Json,Angularjs,Angularjs Ng Repeat,您好,我知道如何读取表单json并将输出绑定到视图,但是我想在输出和bid转换数据中添加一些逻辑。如何将我的forEach输出到数组中,然后基于它进行ng repeat,或者将我的ajax数据绑定与我的修改结合起来 现在如果我改变 $scope.fleet=newData;=>$scope.fleet=数据 和view.html例如{item.name}一切正常,但我想在绑定之前对名称进行一些更改 我的代码: controller.js function LoadFleetControler($
function LoadFleetControler($scope){
$.ajax({
url: 'https://someapi/list',
type: 'GET',
dataType: 'json',
success: function (data) {
var newData = [];
angular.forEach(data, function(value, key){
/* ############################ Options ############################ */
var d = new Date();
var month = d.getMonth() + 1;
var thisDate = d.getDate() + '/' + padLeft(month,2) + '/' + d.getFullYear();
var thisCycle = dateToDays(thisDate, value.end_bill_date) + 1; // Include last 24H
/* ############################ Scope ############################ */
$scope.fleetUser = value.name;
$scope.fleetCycle = 'Cycle: ' + thisCycle + ' days left (' + value.end_bill_date + ')';
$scope.fleetPercentageUsed = value.percentage_used;
$scope.fleetCycleColor = highlighSwitch(value.percentage_used);
}, newData);
console.log(newData);
$scope.fleet = newData;
$scope.$apply();
},
error: function(data) {
$scope.error = true;
$scope.$apply();
}
});
}
view.html
<div ng-controller="LoadFleetControler">
<ons-list>
<ons-list-item ng-show="error">Server Connection Error</ons-list-item>
<ons-list-item class="topcoat-list__item__line-height" ng-repeat="item in fleet">
{{fleetUser}}
<small>{{fleetCycle}}</small>
</ons-list-item>
</ons-list>
</div>
服务器连接错误
{{fleetUser}}
{{fleetCycle}}
在angular.forEach()
内部,您正在将项目分配给作用域,而您可能打算创建新对象并将其添加到新数据
数组中
angular.forEach(data, function(value, key){
// ...
var newItem = {};
newItem.fleetUser = value.name;
newItem.fleetCycle = 'Cycle: ' + thisCycle + ' days left (' + value.end_bill_date + ')';
newItem.fleetPercentageUsed = value.percentage_used;
newItem.fleetCycleColor = highlighSwitch(value.percentage_used);
newData.push(newItem);
}
);
大家好,我得到了您的示例:uncaughttypeerror:Object[Object global]没有“push”方法您还有
var newData=[]吗
在angular.forEach
之前?是的,问题在于这个。push(newItem)让它与newData一起工作。push(newItem);谢谢,还有人想一想如何将HTML添加到角度变量中?例如:newItem.fleetCycleColor=''+value.percentage_used+''$作用域总是将HTML作为文本而不是HTML输出?奇怪。。。如果存在newData
,并将其作为上下文传递到angular.forEach()
,此
应为newData
。。。无论如何,我会更新答案的。谢谢