Set Angular UI Modal height and width programmatically
在这个小sprite中,我有一个Angular UI模态,该模态有一个相关的类
我需要的是在打开模态时以编程方式设置高度和宽度,例如在
我不能使用
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | var app = angular.module("app", ['ui.bootstrap']); app.controller("ctl", function($scope,$uibModal) { /* * these values are taken from a database * and should be used to set the height and width of the modal */ var newHeight = 100; var newWidth = 200; $scope.open = function () { var modalInstance = $uibModal.open({ animation: false, windowClass: 'the-modal', templateUrl: 'myModalContent.html' }); }; }); |
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <style> .the-modal .modal-content{ width:500px; height:300px; } </style> </head> <body ng-app="app" ng-controller="ctl"> <script type="text/ng-template" id="myModalContent.html"> <p>Some content</p> <button type="button" class="btn btn-default" ng-click="open()">Open me!</button> </body> |
将高度和宽度注入模态控制器并使用
JS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var newHeight = 400; var newWidth = 200; $scope.open = function () { var modalInstance = $uibModal.open({ animation: false, windowClass: 'the-modal', templateUrl: 'myModalContent.html', resolve: { height: newHeight, width: newWidth }, controller: function($scope, height, width) { $scope.height = height; $scope.width = width; } }); |
HTML:
1 2 3 | <script type="text/ng-template" id="myModalContent.html"> <p>Some content</p> height: {{height}}, width: {{width}} |
叉形塞
可能不是最好的解决方案,但我认为它可以解决您的问题。
我使用硬编码的高度/宽度值创建了指令
模式模板:
1 2 3 | <script type="text/ng-template" id="myModalContent.html"> <p>Some content</p> |
指令:
1 2 3 4 5 6 7 8 9 10 | app.directive('adjustModal', function() { return { link: function (scope, element) { var height = '100px'; var width = '100px'; element.parent().css("width", width) element.parent().css("height", height) } } }); |
工作塞子
如果可能的话,将您的控制器向上移动到
1 2 3 4 5 6 | <style> .the-modal .modal-content{ width:{{dimension.newWidth}}px; height:{{dimension.newHeight}}px; } </style> |
否则,您可以创建一个指令来调整父元素的大小,例如:punker。
标记:
1 2 3 | <script type="text/ng-template" id="myModalContent.html"> <p>I am {{vm.width}}px wide and {{vm.height}}px high</p> |
JS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | app.directive('parentDimension', [function() { return { restrict: 'A', link: function(scope, elem, attr) { var setDimension = function(dimension, val) { val = val && parseInt(val); if (val) { // Set the dimension of the parent element elem[0].parentNode.style[dimension] = val + 'px'; } }; // Watch for changes in width and set accordingly attr.$observe('parentWidth', function(newVal) { setDimension('width', newVal); }); // Watch for changes in height and set accordingly attr.$observe('parentHeight', function(newVal) { setDimension('height', newVal); }); } } }]); |
哼,将新的样式表行添加到将用于弹出模式的CSS上怎么办;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | var app = angular.module("app", ['ui.bootstrap']); app.controller("ctl", function($scope,$uibModal) { /* * these values are taken from a database * and should be used to set the height and width of the modal */ var newHeight = 100; var newWidth = 200; var style = document.getElementsByTagName("style")[0]; $scope.open = function () { style.sheet.insertRule(".the-modal-changed .modal-content { height:"+newHeight+"px; width:"+newWidth+"px; background:red; }", 0); var modalInstance = $uibModal.open({ animation: false, windowClass: 'the-modal-changed', templateUrl: 'myModalContent.html' }); }; }); |
因此,我获得了当前样式标签的内容,并使用
规则添加了新样式
这样,
工作正常,但由于我确定您所做的事情要比您的演示显示的事情复杂,因此不确定是否适合您的情况。另外,我将背景设为红色。
这是分叉的矮人。