0

下面的代码片段是一个用于$modal.open()的控制器,不知道为什么ok函数不能获取数据绑定改变的最新数据。

myModule.controller('ModalCtrl', function($scope, $modalInstance) {

    $scope.job = 'student'; // changed by html radio button, default is 'student'

    // called by a button with ng-click="ok()" in modal view
    $scope.ok = function() {

        // no matter what job i changed, the $scope.job always is default value 'student'
        $modalInstance.close({
            selected_job : $scope.job
        });
    };
});

这是html片段

<script type="text/ng-template" id="sendToModal.html">
<div>
        <div class="modal-header">
            <h4>Job</h4>
        </div>
        <div class="modal-body">
            <label class="radio-inline">
                <input type="radio" name="jobOption"
                    data-ng-model="job"
                    data-ng-value="student">Student
            </label>
            <label class="radio-inline">
                <input type="radio" name="jobOption"
                    data-ng-model="job"
                    data-ng-value="teachch">Teacher
            </label>
        <div class="modal-footer">
            <button class="btn btn-primary" data-ng-click="ok()">Confirm</button>
            <button class="btn btn-warning" data-ng-click="cancel()">Cancel</button>
        </div>
</div>
</script>
4

3 回答 3

2

问题在于范围。观点变化:

data-ng-model="$parent.job"
于 2013-12-18T10:21:43.477 回答
1

您必须将其作为对象添加到范围:

$scope.job = { name : '学生' }

数据-ng-model="job.name"

在这里,我给你举了一个例子: http ://plnkr.co/edit/NeUwWNYS8dfyQlrR2xfn?p=preview

于 2013-11-06T16:37:28.047 回答
0

我在这部分代码中没有看到错误......除了函数之外的所有绑定都有效?您是否添加了 ctrl、模型等...?你在IE8上吗?

也许试试这个(它对我有用):

// called by a button with ng-click="ok()" in modal view
    ok = function() { ... }
/*--------------------------*/
//in the view call
    onclick="ok();"
于 2013-10-25T07:59:01.623 回答