【发布时间】:2014-04-01 03:55:27
【问题描述】:
尝试设置 Angular 工厂/服务来使用 API 数据,这样我就可以构建默认索引和显示页面。
我的问题是数据没有从工厂返回到控制器和视图。
我的第二个问题是当我点击客户时,它应该再次查询 API 并返回相关客户。但是我对如何通过customerID 传递routeParams 感到困惑
app.factory('apiFactory', ["$http", "$routeParams", ($http, $routeParams) ->
factory = {}
factory.getCustomers = ->
$http(
method: "GET"
url: "/customers.json"
).success( (data) ->
data
).error( (status) ->
console.log "Error"
factory.getCustomer = (customerId) ->
customerId = $routeParams.customerID
customer
factory
])
app.controller("CustomersController", ["$scope", "apiFactory", ($scope, apiFactory) ->
$scope.customers = apiFactory.getCustomers()
console.log $scope.customers
$scope.customer = apiFactory.getCustomer
console.log $scope.customer
])
在以下示例中,我将 url 替换为静态 json 文件,但也没有运气。 这里是对应的 Plunker http://plnkr.co/edit/G6eFwR7uCNu32cLa1MvV?p=preview
【问题讨论】:
-
你可以在这里看看我的回答:stackoverflow.com/questions/16930473/…
标签: javascript angularjs http