在角内windowTemplateUrl访问范围范围、windowTemplateUrl

2023-09-13 03:21:29 作者:23.欢囍

我有一个windowTemplateUrl我的模式如下

I have a windowTemplateUrl for my modal as follow

<div tabindex="-1" role="dialog" class="modal fade" ng-class="{in: animate}" ng-style="{'z-index': 1050 + index*10, display: 'block'}" data-ng-click="close($event)">
    <div class="modal-dialog" ng-class="{'modal-sm': size == 'sm', 'modal-lg': size == 'lg'}">
        <div class="modal-content square-btn">
            <div class="modal-header custom-modal-header square-btn">
                <button type="button" class="close" data-dismiss="modal" ng-click="cancel($event)">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Close</span>
                </button>
                <h3 class="modal-title">{{modalTitle}}--</h3>
            </div>
            <div class="modal-body" modal-transclude>

            </div>            
        </div>
    </div>
</div>

和我的js code是如下

And my js code is as follow

$modal.open({
            windowTemplateUrl: 'templates/common/modal.html',            
            templateUrl: 'templates/jobs/create-job-modal.html',
            resolve: {
                modalTitle: function(){
                    return 'Create new position';
                }
            },
            controller: ['$scope', 'modalTitle', function( $scope, modalTitle ) {
                $scope.modalTitle = modalTitle;
            }]
        });

但似乎我的范围是不是从 modal.html 模板访问。但我可以从创建在职modal.html 模板访问它。我需要它里面modal.html。

But it seems that my scope is not accessible from modal.html template. But i can access it from create-job-modal.html template. I need it inside modal.html.

我怎样才能做到这一点。在此先感谢

How can i achieve this. Thanks in advance

推荐答案

这是一种反直觉的,但在控制器的范围实际上是在windowTemplate的父范围。结果所以,你可以通过在windowTemplate做$ parent.modalTitle访问modalTitle。

It is kind of counter-intuitive but the scope of the controller is actually the parent scope of the windowTemplate. So you can access modalTitle by doing $parent.modalTitle in your windowTemplate.