AngularJS Bootstrap Modal Losing Scope
AngularJS Bootstrap Modal Losing Scope
我有一个由ngRoute加载的模板,其中包含一个引导模态对话框。当用户试图离开他们正在表中编辑的当前行时,会弹出这个对话框,以确认他们是否可以丢失他们的更改。
Bootstrap模式的注入覆盖在身体水平,而我的观点是比那更深一点。因此,模态被覆盖层隐藏。如果我注入模态到相同的水平,我可以看到覆盖,但现在范围丢失了。
是否有一种方法以某种方式绑定范围或另一种方式注册到事件,以便我可以继续与控制器通信?
这是我尝试的代码笔:http://codepen.io/matthewrhoden1/pen/BzEBxQ在其中,你会发现我放弃的点是试图将作用域发送到模态。
请记住,html是通过这一行在更高的级别注入的:
$('#secondModal').insertBefore('#outerOverlay');
最后我认为我可以附加到根作用域。模态的内容不能是动态的,因为作用域丢失了。至少这样我还能确定是/否。
// The directive needs the $rootScope, the event will propagate down.
// This is a bad practice but in my case I don't have any work arounds.
app.directive('event', ['$rootScope', function($rootScope) {
return {
restrict: 'A',
link: function (scope, element, attr) {
element.bind('click', function(){
$rootScope.$broadcast(attr.eventName);
});
}
};
}]);
// Thanks to bootstrap injecting the backdrop at the body level,
// I need to do this to get my modal at the correct location.
$('#secondModal').insertBefore('#outerOverlay');
加上标记:
<div ng-app="myApp">
<div class="container">
<div class="content">
<div class="ngView">
<div ng-controller="TestCtrl">
<h1>Angular Rendered Template</h1>
<div class="modal">
Static Message
<button event data-event-name="test">
OK
</button>
</div>
<div id="secondModal"
class="modal">
Static message
<button event data-event-name="test">
OK
</button>
</div>
</div>
</div>
</div>
<div class="side-menu">
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
</ul>
</div>
</div>
</div>
<!-- backdrop injected here at bottom of the body -->
相关文章:
- AngularJS:ng之后,重复$scope值未按预期更新
- $ionicplatform内的$scope不;不起作用
- 使用$scope方法时的ControllerAs语法
- 如何将ngrepeat下的ngmodel绑定到$scope
- 另一个if(!$scope.$$phase)$scope$apply()szenario-需要帮助才能通关
- 使用AngularJS Directive WHITOUT$scope创建一个动态html元素
- $scope变量被视为字符串AngularJs
- ng在更新$scope后重复不更新信息
- 正在获取Angular以检测$scope中的更改
- 如何惯用地手动销毁scope&在AngularJS中重新创建
- Understanding Javascript scope with "var that = this&qu
- AngularJS,angular.element($0).scope()揭示了每个控制器的函数
- Ng出口无法访问父$scope
- ng选项-用vm替换$scope
- $scope变量,ng隐藏/显示
- 强制 $scope.$watch 只开火一次
- Ionic:AngularJS变量未使用$scope更新DOM
- Angular$scope在模式窗口内不起作用
- 在将绑定应用于控制器之后,是否会发出Angular$scope事件
- AngularJS Bootstrap Modal Losing Scope