我似乎在使用JSON數據填充AngularJS中的select元素時遇到了一些問題。AngularJS基於字符串的JSON鍵值對
代碼:
/* the API returns: {"id":3,"name":"Some Address"} */
app.service("addressService", function($http, $q) {
var deferred = $q.defer();
$http.get('/index.php/Pro/getAddresses').then(function(data) {
deferred.resolve(data);
});
this.getCounties = function() {
return deferred.promise;
};
});
app.controller('homeController', function($scope, $http, addressService, $routeParams) {
$scope.availableCounties = {};
var promise = addressService.getCounties();
promise.then(function(data) {
$scope.availableCounties = data;
});
});
<select name="sender_address_id" ng-model="sender_address_id" ng-options="county.id as county.name for county in availableCounties">
</select>
當我查看該頁面,選擇欄仍然無人居住。我也可以控制API。如果使用比json_encode()更好的函數或方法,那也可以解決問題。
編輯,控制檯輸出鞋子起來就是:
Object {data: Object, status: 200, headers: function, config: Object, statusText: "OK"} config: Object data: Object id: 3 name: "Some Address" __proto__: Object headers: function (c){a||(a=Xc(b));return c?(c=a[Q(c)],void 0===c&&(c=null),c):a}status: 200 statusText: "OK" __proto__: Object
如果將其登錄到視圖中,則爲{{availableCounties}}'爲空? – DonJuwe 2015-02-10 07:58:50
將數據分配給您的承諾中的availableCounties時,數據的實際外觀如何?它可能需要是'in availableCounties'中用於處理ng-options聲明的一組對象。 – mindparse 2015-02-10 08:01:26
已更新問題。 :| – on3pk 2015-02-10 08:10:08