Ajax调用后的双向数据绑定

时间:2014-06-19 13:44:00

标签: javascript jquery ajax angularjs 2-way-object-databinding

我对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>

单击“编辑”按钮将触发此功能(存在于Controller中):

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.phonenewContact.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>

1 个答案:

答案 0 :(得分:0)

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

//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;

在回调之外。