javascript - Angular Modal 不绑定(bind)到范围属性

标签 javascript angularjs asp.net-mvc twitter-bootstrap angular-ui-bootstrap

我有一个像这样的模型 Controller :

pcApp.controller("ModalInstanceController", function ($scope, $modalInstance, model) {

    $scope.claim = model;
    $scope.payNow = function () {
        $modalInstance.close("now");
    };
    $scope.refuse = function () {
        $modalInstance.close("later");
    };
    $scope.payLater = function () {
        $modalInstance.dismiss("cancel");
    };
});

模态模板是:

<script type="text/ng-template" id="newClaimPopup.html">
    <div class="modal-header">
        <h3 class="desktop">PayCaddy Claim</h3>
    </div>
    <div class="modal-body">
        <p>{{claim.SenderFullName}} is claiming an amount of R{{claim.Amount}} for a golfing bet with him that you lost, with the message:</p>
        <br />
        <p>{{claim.Description}}</p>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" type="button" ng-click="payNow()">Yes</button>
        <button class="btn btn-danger" type="button" ng-click="refuse()">Refuse</button>
        <button class="btn btn-warning" type="button" ng-click="payLater()">Later</button>
    </div>
</script>

这是_Layout.cshtml中包含的部分 View :

    <div id="claim-notice-template">
        @Html.Partial("_NewClaimPopupTemplate")
    </div>

我使用以下代码显示模式:

$scope.showNewClaimPopup = function (viewModel) {
    $scope.claim = viewModel;
    var modalInstance = $modal.open({
        animation: $scope.animationsEnabled,
        templateUrl: "newClaimPopup.html",
        controller: "ModalInstanceController",
        size: "sm",
        resolve: {
            model: function () {
                return $scope.claim;
            }
        }
    });

    modalInstance.result.then(function () {
        debugger;
        $log.info("Modal accepted at: " + new Date());
    }, function () {
        $log.info("Modal dismissed at: " + new Date());
    });
};

传递给 $scope.showNewClaimPopupviewModel 参数有效且已完全填充。 openresolve 选项也有效,因为在 ModalInstanceController 中我可以看到 model 参数是相同的有效的 View 模型。然而,当模式显示时,绑定(bind)模板都是空白的。

显示的所有代码都位于分配给包围所有内容(包括包含模式模板的部分)的 div 的一个 MainController 中。

为什么模板没有按预期绑定(bind)?

最佳答案

您需要传递$scope来编译模态模板:

var modalInstance = $modal.open({
    scope: $scope, // <--- this line is necessary
    animation: $scope.animationsEnabled,
    templateUrl: "newClaimPopup.html",
    controller: "ModalInstanceController",
    size: "sm",
    resolve: {
        model: function () {
            return $scope.claim;
        }
    }
});

如果您不提供scope配置,那么模态将创建$rootScope的新子范围,它显然不包含claim 对象。

关于javascript - Angular Modal 不绑定(bind)到范围属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32640171/

相关文章:

angularjs - 使用ngImgCrop上传 Angular 文件

javascript - 为什么 AngularJS include 在这个简单的例子中不起作用?

javascript - agGrid AngularJS 1.x 示例不起作用

asp.net-mvc - ASP.NET MVC 定位从 cshtml 生成的 cs 文件?

asp.net-mvc - MVC 在 _Layout.cshtml 中打印连接字符串

asp.net-mvc - MVC 3 : May I place the controllers in a dll?

javascript - 在 Razor 生成的隐藏字段上强制执行类型,在 JavaScript 中接收变量而不进行强制转换

javascript - 如何使标记在缩放时变大/获取当前缩放值 JVectorMap

javascript - 使用javascript动态更改css类属性

javascript - 将 JSON 加载到 Angular