哟!我有一个控制器,它在$scope
上放置了一堆物品。其中一个范围项$scope.openDialog
通过$mdDialog
打开$mdDialog.show()
。传递到$mdDialog.show
的对象有一个模板,其中包含通过ng-file-upload
项目上传文件的控件,您可以阅读here项目。
我想在退出对话框窗口后,如果在对话框窗口中上传的项目在主控制器中可用。我不确定对话框窗口的控制器是应该引用主myCtrl
控制器还是使用它自己的控制器,以及如何将上传的文件提供给myCtrl
。
这是角度代码:
angular.module('app', ['ngMaterial', 'ngFileUpload'])
.controller('myCtrl', ['$scope', '$mdDialog', 'Upload', function($scope, $mdDialog, Upload) {
var tmpl = "<md-dialog>\n" +
"<md-dialog-content>\n" +
" <input type=\"text\" ng-model=\"username\"></br></br>\n" +
" <input type=\"checkbox\" ng-model=\"multiple\">upload multiple file</br></br>\n" +
" watching model:\n" +
" <div class=\"button\" ngf-select ng-model=\"files\" ngf-multiple=\"multiple\">Select File</div>\n" +
" on file change:\n" +
" <div class=\"button\" ngf-select ngf-change=\"upload($files)\" ngf-multiple=\"multiple\">Select File</div>\n" +
" Drop File:\n" +
" <div ngf-drop ngf-select ng-model=\"files\" class=\"drop-box\" \n" +
" ngf-drag-over-class=\"dragover\" ngf-multiple=\"true\" ngf-allow-dir=\"true\"\n" +
" accept=\"image/*,application/pdf\">Drop pdfs or images here or click to upload</div>\n" +
" <div ngf-no-file-drop>File Drag/Drop is not supported for this browser</div>\n" +
" Image thumbnail: <img ngf-src=\"files[0]\">\n" +
" Files:\n" +
" <ul>\n" +
" <li ng-repeat=\"f in files\" style=\"font:smaller\">{{f.name}}</li>\n" +
" </ul>\n" +
" Upload Log:\n" +
" <pre>{{log}}</pre>\n" +
"<md-action><div class=\"button\" ng-click=\"close()\">close!</div></md-action>\n" +
"<md-action><div class=\"button\" ng-click=\"upload()\">upload!</div></md-action>\n" +
"</md-dialog-content>\n" +
"</md-dialog>";
$scope.files = ['files should appear here', 'files 1', 'file2'];
$scope.openDialog = function () {
$mdDialog.show({
parent: angular.element(document.body),
template: tmpl,
controller: 'myCtrl'
});
};
$scope.close = function() {
$mdDialog.hide();
};
$scope.$watch('files', function () {
$scope.upload($scope.files);
});
$scope.upload = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
Upload.upload({
url: 'upload/url',
fields: {'username': $scope.username},
file: file
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
}).success(function (data, status, headers, config) {
console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
});
}
}
};
}]);
BTW:作为看家记录,我经常听到不应该将应用程序逻辑传递给控制器。在这种情况下,如果它引用$scope.upload
并且$scope
在工厂中不可用,您将如何将$scope
移动到工厂中?
感谢您的帮助。
答案 0 :(得分:11)
您可以将控制器的$ scope传递给$ mdDialog,如下面的示例
$mdDialog.show({
parent: angular.element(document.body),
template: tmpl,
scope: $scope,
controller: 'myCtrl'
});
检查plunkr:http://plnkr.co/edit/0hFWEyWdetTXcPLPkbmQ?p=preview
要将应用程序逻辑移至工厂,您将执行类似这样的操作
$scope.upload = factory.upload(files,$scope.username);
并且工厂将有方法
factory.upload = function(files,username)
{
function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
Upload.upload({
url: 'upload/url',
fields: {'username': username},
file: file
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
}).success(function (data, status, headers, config) {
console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
});
}
}
};
答案 1 :(得分:3)
在$ mdDialog.show()中设置scope: $scope
会将范围带入模态,preserveScope: true
应该将新添加的元素保留到$ scope,否则它会在之后删除它们?
$mdDialog.show({
template: tmpl,
scope: $scope,
preserveScope: true,
controller: 'myCtrl'
});