Ajax调用后的双向数据绑定



我对AngularJS的双向数据绑定有一个问题。我会尽可能清楚地解释这个问题:我有一份联系人名单。对于每个元素,我都有一个Edit按钮。当我单击该按钮时,我从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模型反应良好,就好像双向数据绑定从视图到模型工作得很好,而不是相反。提前感谢!

EDIT:这是检索联系人的服务:

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>

最终我发现了错误。这是我的一个错误,它不属于AngularJS,而是属于Javascript:你会注意到,在Contact服务中,我做了:

//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;
    },
    function(){
        contactPromise.reject("Error");
    }
);

显然,在写this.回调函数时,我并没有影响Contact值,而是影响了函数属性!为了解决这个问题,我不得不这样改变回调:

//the Ajax call    
var metacontact = ContactDBServices.find({ID:contactID}, 
    function(){
        self.ID = contactID;
        self.firstName = metacontact.contact_name;
        self.lastName = metacontact.contact_last_name;
        self.phone = metacontact.contact_phone;
        self.fax = metacontact.contact_fax;
        self.mobile = metacontact.contact_mobile;
        self.email = metacontact.contact_email;
        self.web = metacontact.contact_web;
    },
    function(){
        contactPromise.reject("Error");
    }
);

,

var self = this;

最新更新