【发布时间】:2014-06-19 13:44:00
【问题描述】:
我对 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>
【问题讨论】:
-
newContact.onLoad()正确吗? -
是的,它是一个返回角度承诺的函数。模式在 Ajax 调用结束时正确显示,检索到的 Json 包含正确的值,但这些值不是与视图“绑定的双向数据”。
-
你的模态是如何连接的?它的控制器是否共享您使用
newContact填充的$scope对象? -
模态框所在的html页面有一个独特的控制器,叫做“customerController”。模态与整个页面共享 $scope。
-
我刚刚编辑了我的问题,也许现在更完整了。感谢您的帮助。
标签: javascript jquery ajax angularjs 2-way-object-databinding