ng-model不会更新,直到我再次触发模态

时间:2018-06-29 11:08:20

标签: javascript angularjs bootstrap-modal

我有一个非常基本的引导程序模式,当用户单击“确定”时,它将$scope.accountID分配给一个值。

它运行完美(它在控制台上打印出我想要的值)

但是ng模型不会更新,直到我再次触发模态,并且{{accountID}}仅在再次打开模态后才更新。

这是什么?

控制器

 // when user selects a row from the table
$scope.selectCustomer = function (customer) {
    BootstrapDialog.confirm({
        title: 'Confirm customer selection',
        message: 'Are you sure that you want to select the customer <b>' + customer.FIRST_NAME + " " + customer.LAST_NAME + "</b> with Account ID " + customer.ACCOUNT_ID + "?",
        type: BootstrapDialog.TYPE_INFO, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
        closable: true, // <-- Default value is false
        draggable: true, // <-- Default value is false
        btnCancelLabel: 'Cancel', // <-- Default value is 'Cancel',
        btnOKLabel: 'Ok', // <-- Default value is 'OK',
        btnOKClass: 'btn-primary', // <-- If you didn't specify it, dialog type will be used,
        callback: function(result) {
            // result will be true if button was click, while it will be false if users close the dialog directly.
            if(result) {
                $scope.accountID = customer.ACCOUNT_ID;
                console.log(customer.ACCOUNT_ID); // this prints correctly
            }
        }
    });
};

HTML

<pre>{{accountID}}</pre> // this only updates when I open the modal, click ok and then open modal again

1 个答案:

答案 0 :(得分:0)

此行

$scope.accountID = customer.ACCOUNT_ID;

定义$scope.accountID,但是您可以在callback中进行定义。 callback是在完成作业时执行的function。在这种情况下,callback上发生某个事件时,将执行confirm,例如单击按钮或关闭对话框。问题是您想在$scope.accountID对话框打开之后但在单击按钮或关闭对话框之前使用confirm。但这不是在此时间间隔内定义的,您的代码不是按照从上到下的顺序执行的,因为您的回调的执行要晚于其下面的代码。因此,您需要在更早的阶段定义$scope.accountID,在我们的情况下,必须先定义confirm

 // when user selects a row from the table
$scope.selectCustomer = function (customer) {
    $scope.accountID = customer.ACCOUNT_ID;
    BootstrapDialog.confirm({
        title: 'Confirm customer selection',
        message: 'Are you sure that you want to select the customer <b>' + customer.FIRST_NAME + " " + customer.LAST_NAME + "</b> with Account ID " + customer.ACCOUNT_ID + "?",
        type: BootstrapDialog.TYPE_INFO, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
        closable: true, // <-- Default value is false
        draggable: true, // <-- Default value is false
        btnCancelLabel: 'Cancel', // <-- Default value is 'Cancel',
        btnOKLabel: 'Ok', // <-- Default value is 'OK',
        btnOKClass: 'btn-primary', // <-- If you didn't specify it, dialog type will be used,
        callback: function(result) {
            // result will be true if button was click, while it will be false if users close the dialog directly.
            if(result) {
                console.log(customer.ACCOUNT_ID); // this prints correctly
            }
        }
    });
};