无需手动将模板加载到对话框中。$dialog
来自 AngularUI的服务接受静态模板或模板 url。该 URL 可以返回任何内容,它只会GET
通过 AJAX 调用执行请求,并用返回的任何数据填充对话框。该请求在本地缓存,因此下一个调用应该比第一个调用更快。
这是一个简单的片段,可以帮助您入门:
Javascript
angular.module('app', ['ui.bootstrap.dialog'])
.controller('Ctrl', function($scope, $dialog, $window) {
$scope.open = function() {
var options = {
backdrop: true,
keyboard: true,
controller: 'DialogCtrl', // the dialog object will be inject
// into it
templateUrl: 'path/to/view' // can be either a static file or
// a service that returns some data
};
var dialog = $dialog.dialog(options);
dialog.open().then(function(result) {
if (result === 0) {
$window.alert("Cancel pressed");
}
else if (result === 1) {
$window.alert("OK pressed");
}
});
};
})
.controller('DialogCtrl', function($scope, $http, dialog) {
// Here you can put whatever behavior the dialog might have
$scope.close = function(result) {
dialog.close(result);
};
// Loads some data into the dialog scope
$http.get('/path/to/service')
.success(function(response) {
$scope.items = response;
});
});
主要 HTML
<div ng-app="app" ng-controller="Ctrl">
<button ng-click="open()">Open dialog</button>
</div>
查看 HTML
<div class="modal-header">
<h3>Title</h3>
</div>
<div class="modal-body">
<!-- Renders the data loaded by the controller -->
<p ng-repeat="item in items">{{ item }}</p>
</div>
<div class="modal-footer">
<button class="btn" ng-click="close(0)">Cancel</button>
<button class="btn btn-primary" ng-click="close(1)">OK</button>
</div>
这个Plunker 脚本演示了以上所有内容。
更新:我更新了示例代码,以演示如何动态更改对话框内容。