[英]AngularJS Binding Issue on Complex Object
我從MVC REST服務返回了此視圖模型:
[DataContract]
public class AccountViewModel
{
[DataMember]
public IEnumerable<string> Currencies { get; set; }
[DataMember]
public IEnumerable<string> FromAccounts { get; set; }
[DataMember]
public IEnumerable<string> ToAccounts { get; set; }
}
我有這個角度:
angular.module('accountServices', ['ngResource'])
.factory('Accounts', function($resource) {
return $resource('/SomeUrl/Accounts', {}, {
get: { method: 'GET' }
});
});
angular.module('staticDataServices', ['accountServices'])
.service('StaticData', function (Accounts) {
self.AccountViewModel = Accounts.get();
self.Reasons = ['Incorrect Alloc',
'Unallocated',
'Client Withdrawal',
'Margin Topup',
'Margin Reduction',
'Other'];
return self;
});
webtraderDeposits.controller('ApprovalDialogController', ['$scope', 'dialog', 'item', 'StaticData', 'Deposits',
function ($scope, dialog, item, StaticData, Deposits) {
$scope.AccountViewModel = StaticData.AccountViewModel;
$scope.Reasons = StaticData.Reasons;
alert(StaticData.AccountViewModel.Currencies);
} ]);
StaticData.AccountViewModel
是一個對象,但是StaticData.AccountViewModel.Currencies
是未定義的。 關於我在做什么錯的任何想法嗎?
該Web服務返回以下數據:
<AccountViewModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebtraderBackOffice.RESTService.Models">
<Currencies xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:string>USD</d2p1:string>
</Currencies>
<FromAccounts xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:string>59500/FUSD</d2p1:string>
</FromAccounts>
<ToAccounts xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:string>59504/6227905</d2p1:string>
<d2p1:string>59504/6227925</d2p1:string>
<d2p1:string>59504/6227951</d2p1:string>
<d2p1:string>59504/6227958</d2p1:string>
<d2p1:string>59504/6227959</d2p1:string>
<d2p1:string>59505/50203040</d2p1:string>
<d2p1:string>59505/6567866</d2p1:string>
</ToAccounts>
那個StaticData.AccountViewModel
是一個對象,不必表示它就是您期望的對象。 由於self.AccountViewModel = Accounts.get();
,AccountViewModel是相當多保證是一個對象,或者至少東西 ,無論Accounts.get()
返回。 Accounts.get()
應該返回一個Promise對象,其中包含您從服務器獲取的Angular應用程序,所以這就是您需要查看的內容。
如果您的REST服務返回XML,那么顯然是問題所在。 Angular采用JSON,因此不會將XML解析為任何對您有用的東西。 理想情況下,您應該使Web服務返回JSON而不是XML。 具體操作取決於您的Web服務。 對於大多數現代的Web框架,這是相當瑣碎的,但是我不知道您使用的是哪種Web框架。 如果需要答案,請分享有關此信息的更多信息。
如果您不控制Web服務或無法使其發送JSON,則不會丟失所有內容。 有多種方法可以使Angular解析XML。 例如,請參見以下問題: 如何在AngularJS中處理XML服務? 或此博客文章: http : //rabidgadfly.com/2013/02/angular-and-xml-no-problem/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.