1.使用transformResponse来改写响应
$http.get(url, { transformResponse: function(data) { // 将响应转换为JSON格式 var response = JSON.parse(data); // 对于每个数组元素,将其中的某个属性更改为新的值 angular.forEach(response.array, function(element) { element.property = "new value"; }); // 返回修改后的响应 return response; } }).then(function(response) { // 响应已被修改 console.log(response); });
2.使用ng-repeat中的过滤器来修改显示的数据
// 在HTML中使用过滤器来显示更改后的属性值
// 在控制器中定义一个名为modifyProperty的过滤器 app.filter('modifyProperty', function() { return function(array) { // 对于每个数组元素,将其中的某个属性更改为新的值 angular.forEach(array, function(element) { element.property = "new value"; }); // 返回修改后的数组 return array; }; });