我在 Angular 中有一个返回 JSON 的函数。我怎样才能让它返回值?
类别工厂
angular.module('ExpenseApp')
.factory('CategoryLoader', function ($resource) {
return $resource('http://localhost:3000/categories', {
});
})
Controller 中的函数
CategoryLoader.query(function (response) {
$scope.categories = response;
});
这是我将其绑定(bind)到 ng-model 时得到的结果
"category":"{\"id\":\"2\",\"name\":\"Hotel/Transportation\"}"
这就是我想要的。
"category\":\"Hotel/Transportation\"
最佳答案
您可以迭代响应并将数组分配给范围:
categoriesarray=[];
for(var i=0;i<response.length;i++){
categoriesarray[i] = "Category : " + response[i].name;
}
$scope.categories= categoriesarray;
关于javascript - JS 函数返回 JSON。我怎样才能只返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37196903/