- 首页
- 前端开发
- 以角度访问 windowTemplateUrl 内的范围
以角度访问 windowTemplateUrl 内的范围
[英] access scope inside windowTemplateUrl in angular
本文介绍了以角度访问 windowTemplateUrl 内的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模态有一个 windowTemplateUrl 如下
<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">×</span><span class="sr-only">关闭</span>按钮><h3 class="modal-title">{{modalTitle}}--</h3>
<div class="modal-body" modal-transclude>
我的js代码如下
$modal.open({windowTemplateUrl: 'templates/common/modal.html',templateUrl: 'templates/jobs/create-job-modal.html',解决: {modalTitle:函数(){return '创建新职位';}},控制器:['$scope','modalTitle',函数($scope,modalTitle){$scope.modalTitle = modalTitle;}]});
但似乎无法从 modal.html
模板访问我的范围.但我可以从 create-job-modal.html
模板访问它.我需要它在 modal.html 中.
我怎样才能做到这一点.提前致谢
解决方案
这有点违反直觉,但控制器的作用域实际上是 windowTemplate 的父作用域.
因此,您可以通过在 windowTemplate 中执行 $parent.modalTitle 来访问 modalTitle.
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">×</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>
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;
}]
});
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
解决方案
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.
这篇关于以角度访问 windowTemplateUrl 内的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!