我对 AngularJS 双向数据绑定有疑问。我会尽量清楚地解释这个问题:我有一个联系人姓名列表。对于每个元素,我都有一个编辑按钮。当我单击该按钮时,我会从 Ajax 调用加载“已单击”的完整联系人,然后显示一个窗口,其中包含一些链接到刚刚检索到的联系人的输入字段(“电话”、“电子邮件”等)。这是视图中有趣的部分:
<div>
<div class="contact" data-ng-repeat="contact in contacts">
<span>{{contact.label}}</span>
<a href="" class="btn btn-xs btn-default" data-ng-click="openContactModal(contact.ID)">
Edit
</a>
</div>
</div>
单击编辑按钮会触发此功能(存在于控制器中):
var newContact = null;
$scope.openContactModal = function(contactID){
newContact = new Contact(contactID);
newContact.onLoad().then(
function(){
//loading OK
$('#myModal').modal('show');
},
function(){
//loading Error
}
);
$scope.newContact = newContact;
};
通过new Contact(contactID)
Ajax 调用从数据库加载联系人的调用。我在 Ajax 调用结束时打开模式窗口(等待 AngularJS 承诺)。但是,在模式中,所有字段都是空的,即使它们链接到联系人模型(newContact.phone
等newContact.email
)。我已经检查了 Ajax 调用是否正常(打印结果 JSON)。我想我在双向数据绑定问题上遗漏了一些东西。奇怪的事实是,如果我尝试填充空的模态字段,newContact
模型反应良好,好像双向数据绑定从视图到模型工作良好,但并非相反。先感谢您!
编辑:这是检索联系人的服务:
angular.module("app").factory("Contact", ["ContactDBServices", "$q",
function(ContactDBServices, $q){
return function(contactID){
//the contact itself
var self = this;
var contactPromise = $q.defer();
//attributi del contatto
this.firstName = null;
this.ID = null;
this.lastName = null;
this.phone = null;
this.fax = null;
this.mobile = null;
this.email = null;
this.web = null;
//the Ajax call
var metacontact = ContactDBServices.find({ID:contactID},
function(){
this.ID = contactID;
this.firstName = metacontact.contact_name;
this.lastName = metacontact.contact_last_name;
this.phone = metacontact.contact_phone;
this.fax = metacontact.contact_fax;
this.mobile = metacontact.contact_mobile;
this.email = metacontact.contact_email;
this.web = metacontact.contact_web;
//!!!THE FOLLOWING PRINTS ARE GOOD!!!!
console.log(this.ID);
console.log(this.firstName);
console.log(this.lastName);
console.log(this.phone);
console.log(this.fax);
contactPromise.resolve("OK");
},
function(){
contactPromise.reject("Error");
}
);
this.onLoad = function(){
return contactPromise.promise;
};
}
}]);
但是,如果我在控制器中打印相同的值,所有这些值都是undefined
:
var newContact = null;
$scope.openContactModal = function(contactID){
newContact = new Contact(contactID);
newContact.onLoad().then(
function(){
//!!!!!!!!THE FOLLOWING PRINTS ARE ALL UNDEFINED!!!!
console.log(newContact.firstName);
console.log(newContact.lastName);
console.log(newContact.phone);
console.log(newContact.fax);
$('#myModal').modal('show');
},
function(){
//loading Error
}
);
$scope.newContact = newContact;
};
这很奇怪。这似乎是一种同步问题:-/这里要彻底的是模态的一个示例:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h2>Contact</h2>
</div>
<div class="modal-body">
<label>
Name
<input class="form-control" id="new_contact_name" data-ng-model="newContact.firstName" placeholder="Name">
</label>
<!-- ...and so on -->
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-dismiss="modal" data-ng-click="createContact()">Crea</button>
</div>
</div>
</div>